-
Notifications
You must be signed in to change notification settings - Fork 4
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
12 changed files
with
265 additions
and
0 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,23 @@ | ||
# 第九章 动态规划 part05 | ||
● 1049. 最后一块石头的重量 II | ||
● 494. 目标和 | ||
● 474.一和零 | ||
|
||
详细布置 | ||
## 1049. 最后一块石头的重量 II | ||
|
||
本题就和 昨天的 416. 分割等和子集 很像了,可以尝试先自己思考做一做。 | ||
视频讲解:https://www.bilibili.com/video/BV14M411C7oV | ||
https://programmercarl.com/1049.%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8FII.html | ||
## 494. 目标和 | ||
大家重点理解 递推公式:dp[j] += dp[j - nums[i]],这个公式后面的提问 我们还会用到。 | ||
视频讲解:https://www.bilibili.com/video/BV1o8411j73x | ||
https://programmercarl.com/0494.%E7%9B%AE%E6%A0%87%E5%92%8C.html | ||
|
||
## 474.一和零 | ||
通过这道题目,大家先粗略了解, 01背包,完全背包,多重背包的区别,不过不用细扣,因为后面 对于 完全背包,多重背包 还有单独讲解。 | ||
视频讲解:https://www.bilibili.com/video/BV1rW4y1x7ZQ | ||
https://programmercarl.com/0474.%E4%B8%80%E5%92%8C%E9%9B%B6.html | ||
|
||
|
||
|
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,21 @@ | ||
# 1049. 最后一块石头的重量 II | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int lastStoneWeightII(vector<int>& nums) { | ||
int sum = 0; | ||
for (int i = 0; i < nums.size(); i ++ ) { | ||
sum += nums[i]; | ||
} | ||
int target = sum >> 1; | ||
vector<int> dp (1+target,0); | ||
for (int i = 0; i < nums.size(); i ++ ) { | ||
for (int j = target; j >= nums[i]; j -- ) { | ||
dp[j] = max(dp[j],dp[j-nums[i]]+nums[i]); | ||
} | ||
} | ||
return sum - dp[target]*2; | ||
} | ||
}; | ||
``` |
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,27 @@ | ||
# 474.一和零 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int findMaxForm(vector<string>& strs, int m, int n) { | ||
// dp[i][j] 表示子集中最多i个0和j个1的最大子集长度 | ||
vector<vector<int>>dp(1+m,vector<int>(1+n,0)); | ||
// x = count(s.begin(),s.end(),'0'); | ||
// y = s.size() - x; | ||
// dp[i][j] = max(dp[i][j],dp[i-x][j-y]+1) | ||
for (int u = 0; u < strs.size() ; u ++ ) { | ||
string s = strs[u]; | ||
int x = count(s.begin(),s.end(),'0'); | ||
int y = s.size() - x; | ||
for ( int i = m ; i >= x; i -- ) { | ||
for ( int j = n ; j >= y; j -- ) { | ||
dp[i][j] = max(dp[i][j],dp[i-x][j-y]+1); | ||
} | ||
} | ||
} | ||
return dp[m][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,27 @@ | ||
# 494.目标和 | ||
|
||
一开始不会做,一种心的dp推导公式 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int findTargetSumWays(vector<int>& nums, int target) { | ||
int sum = 0; | ||
for (int i = 0; i < nums.size(); i ++ ) { | ||
sum += nums[i]; | ||
} | ||
int left = (target + sum); | ||
if (left & 1) return 0; | ||
left = left >> 1; | ||
vector<int>dp(left+1); | ||
dp[0] = 1; | ||
// dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 | ||
for ( int i = 0; i < nums.size() ; i ++ ) { | ||
for (int j = dp.size() - 1; j >= nums[i] ; j -- ) { | ||
dp[j] += dp[j-nums[i]]; | ||
} | ||
} | ||
return dp[left]; | ||
} | ||
}; | ||
``` |
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 @@ | ||
# 第九章 动态规划part06 | ||
● 完全背包 | ||
● 518. 零钱兑换 II | ||
● 377. 组合总和 Ⅳ | ||
|
||
详细布置 | ||
|
||
力扣上没有纯粹的完全背包的题目,所以大家看本篇了解一下 完全背包的理论 | ||
|
||
后面的两道题目,都是完全背包的应用,做做感受一下 | ||
|
||
## 完全背包 | ||
视频讲解:https://www.bilibili.com/video/BV1uK411o7c9 | ||
https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85.html | ||
|
||
## 518. 零钱兑换 II | ||
视频讲解:https://www.bilibili.com/video/BV1KM411k75j | ||
https://programmercarl.com/0518.%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2II.html | ||
|
||
## 377. 组合总和 Ⅳ | ||
视频讲解:https://www.bilibili.com/video/BV1V14y1n7B6 | ||
https://programmercarl.com/0377.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C%E2%85%A3.html |
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,21 @@ | ||
# 377. 组合总和 Ⅳ | ||
|
||
不会做,抄了随想录 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int combinationSum4(vector<int>& nums, int target) { | ||
// dp[i] 表示总和为i的元素组合的个数 | ||
vector<unsigned int>dp(1+target); | ||
dp[0]=1; | ||
for(int j=0;j<=target;j++){ | ||
for(int i=0;i<nums.size();i++){ | ||
if(j>=nums[i]) | ||
dp[j]+=dp[j-nums[i]]; | ||
} | ||
} | ||
return dp[target]; | ||
} | ||
}; | ||
``` |
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,21 @@ | ||
# 518. 零钱兑换 II | ||
|
||
第一道完全背包的题目,抄了随想录 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int change(int amount, vector<int>& coins) { | ||
// dp[j]:凑成总金额j的货币组合数为dp[j] | ||
vector<int>dp(amount+1); | ||
dp[0]=1; | ||
// dp[j] += dp[j - coins[i]]; | ||
for(int i = 0; i < coins.size(); i ++ ) { | ||
for (int j = coins[i]; j <= amount; j ++ ) { // 完全背包,需要重复计算之前的组合数 | ||
dp[j] += dp[j-coins[i]]; | ||
} | ||
} | ||
return dp[amount]; | ||
} | ||
}; | ||
``` |
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,27 @@ | ||
# 第九章 动态规划part07 | ||
|
||
● 70. 爬楼梯 (进阶) | ||
● 322. 零钱兑换 | ||
● 279.完全平方数 | ||
|
||
详细布置 | ||
|
||
## 70. 爬楼梯 (进阶) | ||
|
||
这道题目 爬楼梯之前我们做过,这次再用完全背包的思路来分析一遍 | ||
|
||
https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85%E7%89%88%E6%9C%AC.html | ||
|
||
## 322. 零钱兑换 | ||
|
||
如果求组合数就是外层for循环遍历物品,内层for遍历背包。 | ||
如果求排列数就是外层for遍历背包,内层for循环遍历物品。 | ||
|
||
这句话结合本题 大家要好好理解。 | ||
视频讲解:https://www.bilibili.com/video/BV14K411R7yv | ||
https://programmercarl.com/0322.%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2.html | ||
|
||
## 279.完全平方数 | ||
本题 和 322. 零钱兑换 基本是一样的,大家先自己尝试做一做 | ||
视频讲解:https://www.bilibili.com/video/BV12P411T7Br | ||
https://programmercarl.com/0279.%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0.html |
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,21 @@ | ||
# 279.完全平方数 | ||
|
||
|
||
喜喜这个自己做的 | ||
```cpp | ||
class Solution { | ||
public: | ||
int numSquares(int n) { | ||
// dp[i] 和为 i 的完全平方数的最少数量 | ||
// dp[j] = min(dp[j],dp[j-i*i]+1) | ||
vector<int>dp(1+n,INT_MAX); | ||
dp[0]=0; | ||
for(int i = 1; i * i <= n; i ++ ) { | ||
for(int j = i*i; j <= n; j ++ ) { | ||
dp[j] = min(dp[j],dp[j-i*i]+1); | ||
} | ||
} | ||
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,24 @@ | ||
# 322. 零钱兑换 | ||
|
||
抄了随想录 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
// if (amount == 0) return 0; | ||
// dp[i] -> 可以凑成i所需的 最少的硬币个数 | ||
// dp[i] = min(dp[i],dp[i-coins[j]]+1) | ||
vector<int>dp(amount+1,INT_MAX); | ||
dp[0]=0; | ||
for(int i=0;i<coins.size();i++){ | ||
for(int j=coins[i];j<=amount;j++){ | ||
if(dp[j-coins[i]]!=INT_MAX) | ||
dp[j] = min(dp[j],dp[j-coins[i]]+1); | ||
} | ||
} | ||
return dp[amount]==INT_MAX?-1:dp[amount]; | ||
} | ||
}; | ||
|
||
``` |
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 @@ | ||
# 70. 爬楼梯 (进阶) | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
int m = 2; | ||
// dp[i] -> 爬到有i个台阶的楼顶,有dp[i]种方法 | ||
vector<int> dp(1+n); | ||
dp[0]=1; | ||
for(int i=0;i<=n;i++) { | ||
for(int j=1;j<=m;j++) { | ||
if(i-j>=0) | ||
dp[i]+=dp[i-j]; | ||
} | ||
} | ||
return dp[n]; | ||
} | ||
}; | ||
``` |