-
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
6 changed files
with
119 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,16 @@ | ||
# 第九章 动态规划part03 | ||
|
||
● 343. 整数拆分 | ||
● 96.不同的二叉搜索树 | ||
|
||
详细布置 | ||
|
||
今天两题都挺有难度,建议大家思考一下没思路,直接看题解,第一次做,硬想很难想出来。 | ||
## 343. 整数拆分 | ||
|
||
https://programmercarl.com/0343.%E6%95%B4%E6%95%B0%E6%8B%86%E5%88%86.html | ||
视频讲解:https://www.bilibili.com/video/BV1Mg411q7YJ | ||
## 96.不同的二叉搜索树 | ||
|
||
https://programmercarl.com/0096.%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html | ||
视屏讲解:https://www.bilibili.com/video/BV1eK411o7QA |
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 @@ | ||
# 343. 整数拆分 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int integerBreak(int n) { | ||
// dp[i] -> 对于正整数i 将其拆分为k个数之和的乘机最大值 | ||
vector<int> dp(n+1); | ||
dp[0]=0; | ||
dp[1]=1; | ||
dp[2]=1; | ||
for (int i = 3; i <= n; i ++ ) { | ||
for (int j = 1; j <= i - j; j ++) { | ||
dp[i] = max(dp[i], j * max(i-j,dp[i-j])); | ||
} | ||
} | ||
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,25 @@ | ||
# 96.不同的二叉搜索树 | ||
|
||
想了一下,不会做 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int numTrees(int n) { | ||
vector<int> dp(n+1,0); | ||
if (n<=2)return n; | ||
if (n==3)return 5; | ||
// dp[i] -> 恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种 | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
dp[3] = 5; | ||
for ( int i = 4; i <= n; i ++ ) | ||
for ( int j = 0; j < i; j ++ ) | ||
dp[i] += dp[j]*dp[i-j-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,26 @@ | ||
# 第九章 动态规划part04 | ||
|
||
● 01背包问题,你该了解这些! | ||
● 01背包问题,你该了解这些! 滚动数组 | ||
● 416. 分割等和子集 | ||
|
||
正式开始背包问题,背包问题还是挺难的,虽然大家可能看了很多背包问题模板代码,感觉挺简单,但基本理解的都不够深入。 | ||
|
||
如果是直接从来没听过背包问题,可以先看文字讲解慢慢了解 这是干什么的。 | ||
|
||
如果做过背包类问题,可以先看视频,很多内容,是自己平时没有考虑到位的。 | ||
|
||
背包问题,力扣上没有原题,大家先了解理论,今天就安排一道具体题目。 | ||
|
||
详细布置 | ||
## 01背包问题 二维 | ||
https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html | ||
视频讲解:https://www.bilibili.com/video/BV1cg411g7Y6 | ||
## 01背包问题 一维 | ||
https://programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-2.html | ||
视频讲解:https://www.bilibili.com/video/BV1BU4y177kY | ||
## 416. 分割等和子集 | ||
本题是 01背包的应用类题目 | ||
https://programmercarl.com/0416.%E5%88%86%E5%89%B2%E7%AD%89%E5%92%8C%E5%AD%90%E9%9B%86.html | ||
视频讲解:https://www.bilibili.com/video/BV1rt4y1N7jE | ||
|
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 @@ | ||
# 416. 分割等和子集 | ||
|
||
第一眼不会 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
bool canPartition(vector<int>& nums) { | ||
// dp[i] 容量为s//2的01背包 | ||
int sum = 0; | ||
for (int i = 0; i < nums.size(); i ++ ) { | ||
sum += nums[i]; | ||
} | ||
if (sum & 1) return false; | ||
int target = sum >> 1; | ||
vector<int> dp (1+target); | ||
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 dp[target] == target; | ||
} | ||
}; | ||
``` | ||
一下子想不出转换成01背包的想法 |