Skip to content

Commit

Permalink
20210304动规周末总结:修改周四动规五部曲步骤1中j的状态,部分笔误,优化排版
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyangu committed Dec 24, 2021
1 parent 65732b0 commit 6fddb03
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions problems/周总结/20210304动规周末总结.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

## 周一

[动态规划:买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II(动态规划).html)中股票可以买买多了次
[动态规划:买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II(动态规划).html)中股票可以买卖多次了

这也是和[121. 买卖股票的最佳时机](https://programmercarl.com/0121.买卖股票的最佳时机.html)的唯一区别(注意只有一只股票,所以再次购买前要出售掉之前的股票)

重点在于递推公式公式的不同
重点在于递推公式的不同

在回顾一下dp数组的含义:

Expand Down Expand Up @@ -40,6 +40,7 @@ dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
1. 确定dp数组以及下标的含义

一天一共就有五个状态,

0. 没有操作
1. 第一次买入
2. 第一次卖出
Expand Down Expand Up @@ -117,7 +118,7 @@ for (int j = 0; j < 2 * k - 1; j += 2) {
}
```

**本题和[动态规划:123.买卖股票的最佳时机III](https://programmercarl.com/0123.买卖股票的最佳时机III.html)最大的区别就是这里要类比j为奇数是买,偶数是卖剩的状态**
**本题和[动态规划:123.买卖股票的最佳时机III](https://programmercarl.com/0123.买卖股票的最佳时机III.html)最大的区别就是这里要类比j为奇数是买,偶数是卖的状态**

3. dp数组如何初始化

Expand All @@ -131,7 +132,7 @@ for (int j = 1; j < 2 * k; j += 2) {
}
```

**在初始化的地方同样要类比j为偶数是买、奇数是卖的状态**
**在初始化的地方同样要类比j为奇数是买、偶数是卖的状态**

4. 确定遍历顺序

Expand Down Expand Up @@ -162,9 +163,9 @@ for (int j = 1; j < 2 * k; j += 2) {

j的状态为:

* 1:持有股票后的最多现金
* 2:不持有股票(能购买)的最多现金
* 3:不持有股票(冷冻期)的最多现金
* 0:持有股票后的最多现金
* 1:不持有股票(能购买)的最多现金
* 2:不持有股票(冷冻期)的最多现金

2. 确定递推公式

Expand All @@ -179,7 +180,7 @@ dp[i][2] = dp[i - 1][0] + prices[i];
可以统一都初始为0了。

代码如下:
```
```CPP
vector<vector<int>> dp(n, vector<int>(3, 0));
```
Expand Down

0 comments on commit 6fddb03

Please sign in to comment.