Skip to content

Commit c3a3699

Browse files
committed
1
1 parent 21725f3 commit c3a3699

File tree

9 files changed

+155
-59
lines changed

9 files changed

+155
-59
lines changed

notes/src/SUMMARY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,11 @@
127127
- [day 37](./day37.md)
128128
- [738.单调递增的数字](./day37/lc738.md)
129129
- [968.监控二叉树](./day37/lc968.md)
130+
- [day 38](./day38.md)
131+
- [509. 斐波那契数](./day38/lc509.md)
132+
- [70. 爬楼梯](./day38/lc70.md)
133+
- [746. 使用最小花费爬楼梯](./day38/lc746.md)
134+
- [day 39](./day39.md)
135+
- [62. 不同路径](./day39/lc62.md)
136+
- [63. 不同路径 II](./day39/lc63.md)
130137
- [remains](./remains.md)

notes/src/day38.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 第九章 动态规划part01
2+
3+
● 理论基础
4+
● 509. 斐波那契数
5+
● 70. 爬楼梯
6+
● 746. 使用最小花费爬楼梯
7+
8+
详细布置
9+
10+
今天正式开始动态规划!
11+
12+
## 理论基础
13+
14+
无论大家之前对动态规划学到什么程度,一定要先看 我讲的 动态规划理论基础。
15+
16+
如果没做过动态规划的题目,看我讲的理论基础,会有感觉 是不是简单题想复杂了?
17+
18+
其实并没有,我讲的理论基础内容,在动规章节所有题目都有运用,所以很重要!
19+
20+
如果做过动态规划题目的录友,看我的理论基础 就会感同身受了。
21+
22+
https://programmercarl.com/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html
23+
视频:https://www.bilibili.com/video/BV13Q4y197Wg
24+
## 509. 斐波那契数
25+
26+
很简单的动规入门题,但简单题使用来掌握方法论的,还是要有动规五部曲来分析。
27+
28+
https://programmercarl.com/0509.%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0.html
29+
视频:https://www.bilibili.com/video/BV1f5411K7mo
30+
## 70. 爬楼梯
31+
32+
本题大家先自己想一想, 之后会发现,和 斐波那契数 有点关系。
33+
34+
https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF.html
35+
视频:https://www.bilibili.com/video/BV17h411h7UH
36+
## 746. 使用最小花费爬楼梯
37+
38+
这道题目力扣改了题目描述了,现在的题目描述清晰很多,相当于明确说 第一步是不用花费的。
39+
40+
更改题目描述之后,相当于是 文章中 「拓展」的解法
41+
42+
https://programmercarl.com/0746.%E4%BD%BF%E7%94%A8%E6%9C%80%E5%B0%8F%E8%8A%B1%E8%B4%B9%E7%88%AC%E6%A5%BC%E6%A2%AF.html
43+
视频讲解:https://www.bilibili.com/video/BV16G411c7yZ

notes/src/day38/lc509.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 509. 斐波那契数
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
int fib(int n) {
7+
vector <int> dp = vector<int>(n+1,0);
8+
if (n <= 1) return n;
9+
dp[0] = 0;
10+
dp[1] = 1;
11+
for ( int i = 2; i < n+1; i++) {
12+
dp[i]=dp[i-1]+dp[i-2];
13+
}
14+
return dp[n];
15+
}
16+
};
17+
```

notes/src/day38/lc70.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 70. 爬楼梯
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
// 1-> 1; 2 -> 2; 3-> 3
7+
int climbStairs(int n) {
8+
vector<int>dp(n);if(n<=3)return n;
9+
dp[0]=1;dp[1]=2;for(int i=2;i<n;i++) dp[i] = dp[i-1] + dp[i-2];
10+
return dp[n-1];
11+
}
12+
};
13+
```

notes/src/day38/lc746.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 746. 使用最小花费爬楼梯
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
int minCostClimbingStairs(vector<int>& cost) {
7+
// dp[i] => 到达i的最小花费
8+
// dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2])
9+
int n=cost.size();
10+
vector<int>dp(n+1);
11+
dp[0]=0;dp[1]=0;
12+
for(int i=2;i<n+1;i++)dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
13+
return dp[n];
14+
}
15+
};
16+
```

notes/src/day39.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 第九章 动态规划part02
2+
3+
● 62.不同路径
4+
● 63. 不同路径 II
5+
6+
今天开始逐渐有 dp的感觉了,题目不多,就两个 不同路径,可以好好研究一下
7+
8+
详细布置
9+
10+
## 62.不同路径
11+
12+
本题大家掌握动态规划的方法就可以。 数论方法 有点非主流,很难想到。
13+
14+
https://programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html
15+
视频讲解:https://www.bilibili.com/video/BV1ve4y1x7Eu
16+
17+
## 63. 不同路径 II
18+
19+
https://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.html
20+
视频讲解:https://www.bilibili.com/video/BV1Ld4y1k7c6

notes/src/day39/lc62.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 62. 不同路径
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
int uniquePaths(int m, int n) {
7+
vector<vector<int>>dp(m, vector<int>(n));
8+
// dp[i][j] 到达坐标ij的不同路径树
9+
// dp[i][j] = (i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0);
10+
for(int j=0;j<n;j++)dp[0][j]=1;
11+
for(int i=1;i<m;i++)for(int j=0;j<n;j++){
12+
dp[i][j] = (i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0);
13+
}
14+
return dp[m-1][n-1];
15+
}
16+
};
17+
```

notes/src/day39/lc63.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 63. 不同路径 II
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
7+
// dp[i][j] 到达坐标ij的不同路径树
8+
// dp[i][j] = obstacleGrid[i][j]==1?0:(i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0);
9+
int m=obstacleGrid.size();int n=obstacleGrid[0].size();
10+
vector<vector<int>>dp(m, vector<int>(n,0));
11+
int cnt = 1;
12+
for(int j=0;j<n;j++){
13+
if (obstacleGrid[0][j]==1)cnt = 0;
14+
dp[0][j]=cnt;
15+
}
16+
for(int i=1;i<m;i++)for(int j=0;j<n;j++){
17+
dp[i][j] = obstacleGrid[i][j]==1?0:(i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0);
18+
}
19+
return dp[m-1][n-1];
20+
}
21+
};
22+
```

notes/src/remains.md

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +0,0 @@
1-
# 968.监控二叉树
2-
3-
```cpp
4-
class Solution {
5-
public:
6-
int cnt = 0;
7-
bool f(TreeNode*r) { // should not be null
8-
// if(!r)return true;
9-
if(!r->left&&!r->right)return false; // no monitor
10-
bool lres = false;
11-
if(r->left)res=f(r->left);
12-
if(!res&&r->right)res=f(r->right);
13-
if(res == true) return false;
14-
else {
15-
cnt ++ ;
16-
return true;
17-
}
18-
}
19-
int minCameraCover(TreeNode* r) {if(!r->left&&!r->right)return 1;
20-
f(r);
21-
return cnt;
22-
}
23-
};
24-
```
25-
26-
想不明白:在二叉树中如何从低向上推导呢?
27-
28-
```cpp
29-
class Solution {
30-
public:
31-
int cnt = 0;
32-
int f(TreeNode*p) {
33-
// 0 -- 没有覆盖
34-
// 1 -- 有覆盖了
35-
// 2 -- 有摄像头
36-
if(!p)return 1;
37-
int l = f(p->left);
38-
int r = f(p->right);
39-
if (l==1 && r==1) return 0;//////
40-
if (l==0 || r==0) {//////////////
41-
cnt ++ ;
42-
return 2;
43-
}
44-
return 1;
45-
}
46-
int minCameraCover(TreeNode* r) {if(!r->left&&!r->right)return 1;
47-
if(f(r)==0)cnt++;///////
48-
return cnt;
49-
}
50-
};
51-
```
52-
53-
[0,0,null,null,0,0,null,null,0,0]
54-
0
55-
0 n
56-
n 0
57-
0 n
58-
n 0
59-
0

0 commit comments

Comments
 (0)