-
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
18 changed files
with
431 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,20 @@ | ||
# 第九章 动态规划part09 | ||
● 198.打家劫舍 | ||
● 213.打家劫舍II | ||
● 337.打家劫舍III | ||
|
||
详细布置 | ||
|
||
今天就是打家劫舍的一天,这个系列不算难,大家可以一口气拿下。 | ||
|
||
## 198.打家劫舍 | ||
视频讲解:https://www.bilibili.com/video/BV1Te411N7SX | ||
https://programmercarl.com/0198.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8D.html | ||
|
||
## 213.打家劫舍II | ||
视频讲解:https://www.bilibili.com/video/BV1oM411B7xq | ||
https://programmercarl.com/0213.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8DII.html | ||
|
||
## 337.打家劫舍III | ||
视频讲解:https://www.bilibili.com/video/BV1H24y1Q7sY | ||
https://programmercarl.com/0337.%E6%89%93%E5%AE%B6%E5%8A%AB%E8%88%8DIII.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,23 @@ | ||
# 198. 打家劫舍 | ||
|
||
这个是抄随想录的 | ||
|
||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int rob(vector<int>& nums) { | ||
if (nums.size() == 0) return 0; | ||
if (nums.size() == 1) return nums[0]; | ||
// dp[i]:考虑下标i(包括i)以内的房屋,最多可以偷窃的金额为dp[i] | ||
// dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); | ||
vector<int> dp(nums.size()); | ||
dp[0] = nums[0]; | ||
dp[1] = max(nums[0], nums[1]); | ||
for (int i = 2; i < nums.size(); i++) { | ||
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); | ||
} | ||
return dp[nums.size() - 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,33 @@ | ||
## 213. 打家劫舍 II | ||
|
||
这个是自己写的 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int rob(vector<int>& nums) { | ||
if (nums.size() == 0) return 0; | ||
if (nums.size() == 1) return nums[0]; | ||
// dp[i]:考虑下标i(包括i)以内的房屋,最多可以偷窃的金额为dp[i] | ||
// dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); | ||
vector<int> dp(nums.size()); | ||
// skip index 0 first | ||
int res = 0; | ||
dp[0] = 0; | ||
dp[1] = nums[1]; | ||
for (int i = 2; i < nums.size(); i++) { | ||
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); | ||
} | ||
res = dp[nums.size()-1]; | ||
dp[0] = nums[0]; | ||
dp[1] = max(nums[0], nums[1]); | ||
|
||
for (int i = 2; i < nums.size() - 1; i++) { | ||
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); | ||
} | ||
|
||
res = max(res,dp[nums.size()-2]); | ||
return res; | ||
} | ||
}; | ||
``` |
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,37 @@ | ||
# 337.打家劫舍III | ||
|
||
自己做的 | ||
|
||
```cpp | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
// <r1,r2> r1 如果偷当前节点,得到的最大值 ; r2 不偷当前节点的最大值 | ||
pair<int,int> f(TreeNode*root) { | ||
if(!root){return make_pair(0,0);} | ||
auto leftr = f(root->left); | ||
auto rightr = f(root->right); | ||
int takeleft = leftr.first; | ||
int notakeleft = leftr.second; | ||
int takeright = rightr.first; | ||
int notakeright = rightr.second; | ||
int r1 = notakeleft + notakeright + root->val; | ||
int r2 = max(takeleft,notakeleft) + max(takeright,notakeright); | ||
return make_pair(r1, r2); | ||
} | ||
int rob(TreeNode* root) { | ||
auto res = f(root); | ||
return max(res.first, res.second); | ||
} | ||
}; | ||
``` |
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 @@ | ||
# 第九章 动态规划part10 | ||
|
||
● 121. 买卖股票的最佳时机 | ||
● 122.买卖股票的最佳时机II | ||
|
||
详细布置 | ||
|
||
股票问题是一个动态规划的系列问题,今日安排的题目不多,大家可以慢慢消化。 | ||
|
||
## 121. 买卖股票的最佳时机 | ||
视频讲解:https://www.bilibili.com/video/BV1Xe4y1u77q | ||
https://programmercarl.com/0121.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA.html | ||
|
||
|
||
|
||
|
||
|
||
## 122.买卖股票的最佳时机II | ||
视频讲解:https://www.bilibili.com/video/BV1D24y1Q7Ls | ||
https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAII%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.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,20 @@ | ||
# 121. 买卖股票的最佳时机 | ||
|
||
一开始这样子没有用动规 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
int res = 0; | ||
int cmin = INT_MAX; | ||
for (int i = 0; i < prices.size(); i ++ ) { | ||
cmin = min(cmin, prices[i]); | ||
if (prices[i] > cmin) { | ||
res = max(res, prices[i] - cmin); | ||
} | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |
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,35 @@ | ||
# 122. 买卖股票的最佳时机 II | ||
|
||
抄了随想录 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
int len = prices.size(); | ||
vector<vector<int>> dp(len, vector<int>(2, 0)); | ||
// dp[i][0] 表示第i天持有股票所得现金。 | ||
// dp[i][1] 表示第i天不持有股票所得最多现金 | ||
|
||
// 如果第i天持有股票即dp[i][0], 那么可以由两个状态推出来 | ||
|
||
// 第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0] | ||
// 第i天买入股票,所得现金就是昨天不持有股票的所得现金减去 今天的股票价格 即:dp[i - 1][1] - prices[i] | ||
|
||
|
||
// 再来看看如果第i天不持有股票即dp[i][1]的情况, 依然可以由两个状态推出来 | ||
|
||
// 第i-1天就不持有股票,那么就保持现状,所得现金就是昨天不持有股票的所得现金 即:dp[i - 1][1] | ||
// 第i天卖出股票,所得现金就是按照今天股票价格卖出后所得现金即:prices[i] + dp[i - 1][0] | ||
dp[0][0] -= prices[0]; | ||
dp[0][1] = 0; | ||
for (int i = 1; i < len; i++) { | ||
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); // 注意这里是和121. 买卖股票的最佳时机唯一不同的地方。 | ||
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]); | ||
} | ||
return dp[len - 1][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,21 @@ | ||
# 第九章 动态规划part11 | ||
|
||
● 123.买卖股票的最佳时机III | ||
● 188.买卖股票的最佳时机IV | ||
|
||
详细布置 | ||
|
||
## 123.买卖股票的最佳时机III | ||
|
||
这道题一下子就难度上来了,关键在于至多买卖两次,这意味着可以买卖一次,可以买卖两次,也可以不买卖。 | ||
视频讲解:https://www.bilibili.com/video/BV1WG411K7AR | ||
https://programmercarl.com/0123.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAIII.html | ||
|
||
## 188.买卖股票的最佳时机IV | ||
本题是123.买卖股票的最佳时机III 的进阶版 | ||
视频讲解:https://www.bilibili.com/video/BV16M411U7XJ | ||
https://programmercarl.com/0188.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAIV.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,30 @@ | ||
# 123.买卖股票的最佳时机III | ||
|
||
|
||
自己写的时候漏掉了`dp[0][3]=-prices[0];`这个条件 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
// 一天一共就有五个状态, | ||
|
||
// 没有操作 (其实我们也可以不设置这个状态) | ||
// 第一次持有股票 | ||
// 第一次不持有股票 | ||
// 第二次持有股票 | ||
// 第二次不持有股票 | ||
// dp[i][j]中 i表示第i天,j为 [0 - 4] 五个状态,dp[i][j]表示第i天状态j所剩最大现金。 | ||
vector<vector<int>>dp(prices.size(), vector<int>(5,0)); | ||
dp[0][1]=dp[0][3]=-prices[0]; | ||
|
||
for(int i=1;i<prices.size();i++){ | ||
dp[i][1] = max(dp[i - 1][0] - prices[i], dp[i - 1][1]); | ||
dp[i][2] = max(dp[i - 1][1] + prices[i], dp[i - 1][2]); | ||
dp[i][3] = max(dp[i - 1][2] - prices[i], dp[i - 1][3]); | ||
dp[i][4] = max(dp[i - 1][3] + prices[i], dp[i - 1][4]); | ||
} | ||
return max(dp[prices.size()-1][2],dp[prices.size()-1][4]); | ||
} | ||
}; | ||
``` |
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 @@ | ||
# 188.买卖股票的最佳时机IV | ||
|
||
不过度确实好难啊,不过是自己写的,就类比一下上一道题 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxProfit(int k, vector<int>& prices) { | ||
vector<vector<int>>dp(prices.size(), vector<int>(2*k+1,0)); | ||
for(int j=0;j<k;j++)dp[0][2*j+1]=-prices[0]; | ||
for(int i=1;i<prices.size();i++){ | ||
for(int j=1;j<=k;j++){ | ||
dp[i][2*j-1] = max(dp[i - 1][2*j-2] - prices[i], dp[i - 1][2*j-1]); | ||
dp[i][2*j] = max(dp[i - 1][2*j-1] + prices[i], dp[i - 1][2*j]); // 表示不持有 | ||
} | ||
} | ||
int res = -1; | ||
for(int j=1;j<=k;j++)res=max(res,dp[prices.size()-1][2*j]); | ||
return res; | ||
} | ||
}; | ||
``` |
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 @@ | ||
# 第九章 动态规划part12 | ||
|
||
● 309.最佳买卖股票时机含冷冻期 | ||
● 714.买卖股票的最佳时机含手续费 | ||
●总结 | ||
|
||
## 309.最佳买卖股票时机含冷冻期 | ||
|
||
本题加了一个冷冻期,状态就多了,有点难度,大家要把各个状态分清,思路才能清晰 | ||
视频讲解:https://www.bilibili.com/video/BV1rP4y1D7ku | ||
|
||
https://programmercarl.com/0309.%E6%9C%80%E4%BD%B3%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E6%97%B6%E6%9C%BA%E5%90%AB%E5%86%B7%E5%86%BB%E6%9C%9F.html | ||
|
||
## 714.买卖股票的最佳时机含手续费 | ||
|
||
相对122.买卖股票的最佳时机II ,本题只需要在计算卖出操作的时候减去手续费就可以了,代码几乎是一样的,可以尝试自己做一做。 | ||
视频讲解:https://www.bilibili.com/video/BV1z44y1Z7UR | ||
https://programmercarl.com/0714.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA%E5%90%AB%E6%89%8B%E7%BB%AD%E8%B4%B9%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html | ||
|
||
## 股票总结 | ||
|
||
股票问题做一个总结吧 | ||
https://programmercarl.com/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%82%A1%E7%A5%A8%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93%E7%AF%87.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,23 @@ | ||
# 309.最佳买卖股票时机含冷冻期 | ||
|
||
我自己定义的状态 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& v) { | ||
// dp[i][j] 表示第i天的最大现金 | ||
// 0 持有 | ||
// 1 非持有且不在冷冻期 | ||
// 2 非持有且冷冻期(刚出售) | ||
vector<vector<int>>dp(v.size(),vector<int>(3,0)); | ||
dp[0][0]=-v[0]; | ||
for(int i=1;i<v.size();i++){ | ||
dp[i][0]=max(dp[i-1][0],dp[i-1][1]-v[i]);// 原本就持有 / 解冻后买入 | ||
dp[i][1]=max(dp[i-1][1],dp[i-1][2]);// 原本就不在冷冻期 / 新的解锁 | ||
dp[i][2]=dp[i-1][0]+v[i]; | ||
} | ||
return max(dp[v.size()-1][1],dp[v.size()-1][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,19 @@ | ||
# 714.买卖股票的最佳时机含手续费 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& v, int fee) { | ||
// dp[i][j] | ||
// 0 持有 | ||
// 1 不持有 | ||
vector<vector<int>>dp(v.size(),vector<int>(2,0)); | ||
dp[0][0]=-v[0]; | ||
for(int i=1;i<v.size();i++){ | ||
dp[i][0]=max(dp[i-1][0],dp[i-1][1]-v[i]); | ||
dp[i][1]=max(dp[i-1][1],dp[i-1][0]+v[i]-fee); | ||
} | ||
return max(dp[v.size()-1][0],dp[v.size()-1][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,24 @@ | ||
# 第九章 动态规划part13 | ||
● 300.最长递增子序列 | ||
● 674. 最长连续递增序列 | ||
● 718. 最长重复子数组 | ||
|
||
详细布置 | ||
|
||
## 300.最长递增子序列 | ||
|
||
今天开始正式子序列系列,本题是比较简单的,感受感受一下子序列题目的思路。 | ||
视频讲解:https://www.bilibili.com/video/BV1ng411J7xP | ||
https://programmercarl.com/0300.%E6%9C%80%E9%95%BF%E4%B8%8A%E5%8D%87%E5%AD%90%E5%BA%8F%E5%88%97.html | ||
|
||
## 674. 最长连续递增序列 | ||
|
||
本题相对于昨天的动态规划:300.最长递增子序列 最大的区别在于“连续”。 先尝试自己做做,感受一下区别 | ||
视频讲解:https://www.bilibili.com/video/BV1bD4y1778v | ||
https://programmercarl.com/0674.%E6%9C%80%E9%95%BF%E8%BF%9E%E7%BB%AD%E9%80%92%E5%A2%9E%E5%BA%8F%E5%88%97.html | ||
|
||
## 718. 最长重复子数组 | ||
|
||
稍有难度,要使用二维dp数组了 | ||
视频讲解:https://www.bilibili.com/video/BV178411H7hV | ||
https://programmercarl.com/0718.%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%90%E6%95%B0%E7%BB%84.html |
Oops, something went wrong.