-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
155 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 第九章 动态规划part01 | ||
|
||
● 理论基础 | ||
● 509. 斐波那契数 | ||
● 70. 爬楼梯 | ||
● 746. 使用最小花费爬楼梯 | ||
|
||
详细布置 | ||
|
||
今天正式开始动态规划! | ||
|
||
## 理论基础 | ||
|
||
无论大家之前对动态规划学到什么程度,一定要先看 我讲的 动态规划理论基础。 | ||
|
||
如果没做过动态规划的题目,看我讲的理论基础,会有感觉 是不是简单题想复杂了? | ||
|
||
其实并没有,我讲的理论基础内容,在动规章节所有题目都有运用,所以很重要! | ||
|
||
如果做过动态规划题目的录友,看我的理论基础 就会感同身受了。 | ||
|
||
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 | ||
视频:https://www.bilibili.com/video/BV13Q4y197Wg | ||
## 509. 斐波那契数 | ||
|
||
很简单的动规入门题,但简单题使用来掌握方法论的,还是要有动规五部曲来分析。 | ||
|
||
https://programmercarl.com/0509.%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0.html | ||
视频:https://www.bilibili.com/video/BV1f5411K7mo | ||
## 70. 爬楼梯 | ||
|
||
本题大家先自己想一想, 之后会发现,和 斐波那契数 有点关系。 | ||
|
||
https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF.html | ||
视频:https://www.bilibili.com/video/BV17h411h7UH | ||
## 746. 使用最小花费爬楼梯 | ||
|
||
这道题目力扣改了题目描述了,现在的题目描述清晰很多,相当于明确说 第一步是不用花费的。 | ||
|
||
更改题目描述之后,相当于是 文章中 「拓展」的解法 | ||
|
||
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 | ||
视频讲解:https://www.bilibili.com/video/BV16G411c7yZ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 509. 斐波那契数 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int fib(int n) { | ||
vector <int> dp = vector<int>(n+1,0); | ||
if (n <= 1) return n; | ||
dp[0] = 0; | ||
dp[1] = 1; | ||
for ( int i = 2; i < n+1; i++) { | ||
dp[i]=dp[i-1]+dp[i-2]; | ||
} | ||
return dp[n]; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 70. 爬楼梯 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
// 1-> 1; 2 -> 2; 3-> 3 | ||
int climbStairs(int n) { | ||
vector<int>dp(n);if(n<=3)return n; | ||
dp[0]=1;dp[1]=2;for(int i=2;i<n;i++) dp[i] = dp[i-1] + dp[i-2]; | ||
return dp[n-1]; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 746. 使用最小花费爬楼梯 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minCostClimbingStairs(vector<int>& cost) { | ||
// dp[i] => 到达i的最小花费 | ||
// dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]) | ||
int n=cost.size(); | ||
vector<int>dp(n+1); | ||
dp[0]=0;dp[1]=0; | ||
for(int i=2;i<n+1;i++)dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]); | ||
return dp[n]; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 第九章 动态规划part02 | ||
|
||
● 62.不同路径 | ||
● 63. 不同路径 II | ||
|
||
今天开始逐渐有 dp的感觉了,题目不多,就两个 不同路径,可以好好研究一下 | ||
|
||
详细布置 | ||
|
||
## 62.不同路径 | ||
|
||
本题大家掌握动态规划的方法就可以。 数论方法 有点非主流,很难想到。 | ||
|
||
https://programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html | ||
视频讲解:https://www.bilibili.com/video/BV1ve4y1x7Eu | ||
|
||
## 63. 不同路径 II | ||
|
||
https://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.html | ||
视频讲解:https://www.bilibili.com/video/BV1Ld4y1k7c6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 62. 不同路径 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int uniquePaths(int m, int n) { | ||
vector<vector<int>>dp(m, vector<int>(n)); | ||
// dp[i][j] 到达坐标ij的不同路径树 | ||
// dp[i][j] = (i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); | ||
for(int j=0;j<n;j++)dp[0][j]=1; | ||
for(int i=1;i<m;i++)for(int j=0;j<n;j++){ | ||
dp[i][j] = (i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); | ||
} | ||
return dp[m-1][n-1]; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 63. 不同路径 II | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { | ||
// dp[i][j] 到达坐标ij的不同路径树 | ||
// dp[i][j] = obstacleGrid[i][j]==1?0:(i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); | ||
int m=obstacleGrid.size();int n=obstacleGrid[0].size(); | ||
vector<vector<int>>dp(m, vector<int>(n,0)); | ||
int cnt = 1; | ||
for(int j=0;j<n;j++){ | ||
if (obstacleGrid[0][j]==1)cnt = 0; | ||
dp[0][j]=cnt; | ||
} | ||
for(int i=1;i<m;i++)for(int j=0;j<n;j++){ | ||
dp[i][j] = obstacleGrid[i][j]==1?0:(i>0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); | ||
} | ||
return dp[m-1][n-1]; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +0,0 @@ | ||
# 968.监控二叉树 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int cnt = 0; | ||
bool f(TreeNode*r) { // should not be null | ||
// if(!r)return true; | ||
if(!r->left&&!r->right)return false; // no monitor | ||
bool lres = false; | ||
if(r->left)res=f(r->left); | ||
if(!res&&r->right)res=f(r->right); | ||
if(res == true) return false; | ||
else { | ||
cnt ++ ; | ||
return true; | ||
} | ||
} | ||
int minCameraCover(TreeNode* r) {if(!r->left&&!r->right)return 1; | ||
f(r); | ||
return cnt; | ||
} | ||
}; | ||
``` | ||
想不明白:在二叉树中如何从低向上推导呢? | ||
```cpp | ||
class Solution { | ||
public: | ||
int cnt = 0; | ||
int f(TreeNode*p) { | ||
// 0 -- 没有覆盖 | ||
// 1 -- 有覆盖了 | ||
// 2 -- 有摄像头 | ||
if(!p)return 1; | ||
int l = f(p->left); | ||
int r = f(p->right); | ||
if (l==1 && r==1) return 0;////// | ||
if (l==0 || r==0) {////////////// | ||
cnt ++ ; | ||
return 2; | ||
} | ||
return 1; | ||
} | ||
int minCameraCover(TreeNode* r) {if(!r->left&&!r->right)return 1; | ||
if(f(r)==0)cnt++;/////// | ||
return cnt; | ||
} | ||
}; | ||
``` | ||
|
||
[0,0,null,null,0,0,null,null,0,0] | ||
0 | ||
0 n | ||
n 0 | ||
0 n | ||
n 0 | ||
0 | ||