Skip to content

Commit b0b6a88

Browse files
committed
Add 191 201 231 342
1 parent ba6b338 commit b0b6a88

6 files changed

+373
-220
lines changed

Readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
文章最新首发于微信公众号 **五分钟学算法** ,您可以关注获取最新的文章。
1212

13-
### 汇总
13+
## 汇总
1414

1515
| 序号 | 题目&题解 |
1616
| ---- | ------------------------------------------------------------ |
@@ -51,11 +51,14 @@
5151
| 167 | [两数之和 II - 输入有序数组](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第167号问题:两数之和II-输入有序数组.md) |
5252
| 172 | [阶乘后的零](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第172号问题:阶乘后的零.md) |
5353
| 187 | [重复的 DNA 序列](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第187号问题:重复的DNA序列.md) |
54+
| 191 | [位1的个数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第191号问题:位1的个数.md) |
5455
| 199 | [二叉树的右视图](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第199号问题:二叉树的右视图.md) |
56+
| 201 | [数字范围按位与](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第201号问题:数字范围按位与.md) |
5557
| 203 | [移除链表元素](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第203号问题:移除链表元素.md) |
5658
| 206 | [反转链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第206号问题:反转链表.md) |
5759
| 209 | [长度最小的子数组](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第209号问题:长度最小的子数组.md) |
5860
| 219 | [存在重复元素 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第219号问题:存在重复元素II.md) |
61+
| 231 | [2的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第231号问题:2的幂.md) |
5962
| 237 | [删除链表中的节点](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第237号问题:删除链表中的节点.md) |
6063
| 239 | [滑动窗口最大值](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第239号问题:滑动窗口最大值.md) |
6164
| 279 | [完全平方数](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第279号问题:完全平方数.md) |
@@ -65,6 +68,7 @@
6568
| 326 | [3 的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第326号问题:3的幂.md) |
6669
| 328 | [奇偶链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第328号问题:奇偶链表.md) |
6770
| 344 | [反转字符串](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第344号问题:反转字符串.md) |
71+
| 342 | [4的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第342号问题:4的幂.md) |
6872
| 349 | [两个数组的交集](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第349号问题:两个数组的交集.md) |
6973
| 350 | [两个数组的交集 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第350号问题:两个数组的交集II.md) |
7074
| 445 | [两数相加 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第445号问题:两数相加II.md) |

notes/LeetCode第122号问题:买卖股票的最佳时机II.md

Lines changed: 23 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,15 @@
11

22

3-
# 浅谈什么是动态规划以及相关的「股票」算法题
3+
# LeetCode第122号问题:买卖股票的最佳时机II
44

55
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
66
>
77
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
88
9-
## 动态规划
10-
11-
### 1 概念
12-
13-
  **动态规划**算法是通过拆分问题,定义问题状态和状态之间的关系,使得问题能够以递推(或者说分治)的方式去解决。在学习动态规划之前需要明确掌握几个重要概念。
14-
15-
  **阶段**:对于一个完整的问题过程,适当的切分为若干个相互联系的子问题,每次在求解一个子问题,则对应一个阶段,整个问题的求解转化为按照阶段次序去求解。
16-
17-
  **状态**:状态表示每个阶段开始时所处的客观条件,即在求解子问题时的已知条件。状态描述了研究的问题过程中的状况。
18-
19-
  **决策**:决策表示当求解过程处于某一阶段的某一状态时,可以根据当前条件作出不同的选择,从而确定下一个阶段的状态,这种选择称为决策。
20-
21-
  **策略**:由所有阶段的决策组成的决策序列称为全过程策略,简称策略。
22-
23-
  **最优策略**:在所有的策略中,找到代价最小,性能最优的策略,此策略称为最优策略。
24-
25-
  **状态转移方程**:状态转移方程是确定两个相邻阶段状态的演变过程,描述了状态之间是如何演变的。
26-
27-
### 2 使用场景
28-
29-
能采用动态规划求解的问题的一般要具有 3 个性质:
30-
31-
  (1)**最优化**:如果问题的最优解所包含的子问题的解也是最优的,就称该问题具有最优子结构,即满足最优化原理。子问题的局部最优将导致整个问题的全局最优。换句话说,就是问题的一个最优解中一定包含子问题的一个最优解。
32-
33-
  (2)**无后效性**:即某阶段状态一旦确定,就不受这个状态以后决策的影响。也就是说,某状态以后的过程不会影响以前的状态,只与当前状态有关,与其他阶段的状态无关,特别是与未发生的阶段的状态无关。
34-
35-
   (3)**重叠子问题**:即子问题之间是不独立的,一个子问题在下一阶段决策中可能被多次使用到。(该性质并不是动态规划适用的必要条件,但是如果没有这条性质,动态规划算法同其他算法相比就不具备优势)
36-
37-
### 3 算法流程
38-
39-
  (1)划分阶段:按照问题的时间或者空间特征将问题划分为若干个阶段。
40-
  (2)确定状态以及状态变量:将问题的不同阶段时期的不同状态描述出来。
41-
  (3)确定决策并写出状态转移方程:根据相邻两个阶段的各个状态之间的关系确定决策。
42-
  (4)寻找边界条件:一般而言,状态转移方程是递推式,必须有一个递推的边界条件。
43-
  (5)设计程序,解决问题
44-
45-
## 实战练习
46-
47-
下面的三道算法题都是来源于 LeetCode 上与股票买卖相关的问题 ,我们按照 **动态规划** 的算法流程来处理该类问题。
48-
49-
**股票买卖**这一类的问题,都是给一个输入数组,里面的每个元素表示的是每天的股价,并且你只能持有一支股票(也就是你必须在再次购买前出售掉之前的股票),一般来说有下面几种问法:
50-
51-
- 只能买卖一次
52-
- 可以买卖无数次
53-
- 可以买卖 k 次
54-
55-
需要你设计一个算法去获取最大的利润。
56-
57-
## 买卖股票的最佳时机
58-
59-
题目来源于 LeetCode 上第 121 号问题:买卖股票的最佳时机。题目难度为 Easy,目前通过率为 49.4% 。
9+
在之前有关 [**动态规划与股票问题一文**](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第122号问题:买卖股票的最佳时机II.md) 中,小吴使用了动态规划的思想进行了分析和写套路代码,但还是有一些小伙伴不是很明白,今天重新拿出一题从另外一个角度进行分析,希望能帮助大家更容易理解。
6010

6111
### 题目描述
6212

63-
给定一个数组,它的第 *i* 个元素是一支给定股票第 *i* 天的价格。
64-
65-
如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
66-
67-
注意你不能在买入股票前卖出股票。
68-
69-
**示例 1:**
70-
71-
```
72-
输入: [7,1,5,3,6,4]
73-
输出: 5
74-
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
75-
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
76-
```
77-
78-
**示例 2:**
79-
80-
```
81-
输入: [7,6,4,3,1]
82-
输出: 0
83-
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
84-
```
85-
86-
### 题目解析
87-
88-
我们按照动态规划的思想来思考这道问题。
89-
90-
#### 状态
91-
92-
**买入(buy)****卖出(sell)** 这两种状态。
93-
94-
#### 转移方程
95-
96-
对于买来说,买之后可以卖出(进入卖状态),也可以不再进行股票交易(保持买状态)。
97-
98-
对于卖来说,卖出股票后不在进行股票交易(还在卖状态)。
99-
100-
只有在手上的钱才算钱,手上的钱购买当天的股票后相当于亏损。也就是说当天买的话意味着损失`-prices[i]`,当天卖的话意味着增加`prices[i]`,当天卖出总的收益就是 `buy+prices[i]`
101-
102-
所以我们只要考虑当天买和之前买哪个收益更高,当天卖和之前卖哪个收益更高。
103-
104-
- buy = max(buy, -price[i]) (注意:根据定义 buy 是负数)
105-
- sell = max(sell, prices[i] + buy)
106-
107-
#### 边界
108-
109-
第一天 `buy = -prices[0]`, `sell = 0`,最后返回 sell 即可。
110-
111-
### 代码实现
112-
113-
```java
114-
class Solution {
115-
public int maxProfit(int[] prices) {
116-
if(prices.length <= 1)
117-
return 0;
118-
int buy = -prices[0], sell = 0;
119-
for(int i = 1; i < prices.length; i++) {
120-
buy = Math.max(buy, -prices[i]);
121-
sell = Math.max(sell, prices[i] + buy);
122-
123-
}
124-
return sell;
125-
}
126-
}
127-
```
128-
129-
130-
131-
## 买卖股票的最佳时机 II
132-
13313
题目来源于 LeetCode 上第 122 号问题:买卖股票的最佳时机 II。题目难度为 Easy,目前通过率为 53.0% 。
13414

13515
### 题目描述
@@ -169,126 +49,50 @@ class Solution {
16949

17050
### 题目解析
17151

172-
#### 状态
173-
174-
**买入(buy)****卖出(sell)** 这两种状态。
175-
176-
#### 转移方程
177-
178-
对比上题,这里可以有无限次的买入和卖出,也就是说 **买入** 状态之前可拥有 **卖出** 状态,所以买入的转移方程需要变化。
179-
180-
- buy = max(buy, sell - price[i])
181-
- sell = max(sell, buy + prices[i] )
182-
183-
#### 边界
184-
185-
第一天 `buy = -prices[0]`, `sell = 0`,最后返回 sell 即可。
186-
187-
### 代码实现
188-
189-
```java
190-
class Solution {
191-
public int maxProfit(int[] prices) {
192-
if(prices.length <= 1)
193-
return 0;
194-
int buy = -prices[0], sell = 0;
195-
for(int i = 1; i < prices.length; i++) {
196-
sell = Math.max(sell, prices[i] + buy);
197-
buy = Math.max( buy,sell - prices[i]);
198-
}
199-
return sell;
200-
}
201-
}
202-
```
203-
204-
52+
我们从实际场景去思考,假设你处于股票市场,你想获得最大收益的话理想操作是什么?
20553

206-
## 买卖股票的最佳时机 III
54+
当然是 **低点买入,高点卖出**
20755

208-
题目来源于 LeetCode 上第 123 号问题:买卖股票的最佳时机 III。题目难度为 Hard,目前通过率为 36.1%
56+
举个简单的数组为例 [100,1,20,81],肉眼扫过去,第二天买第四天卖的话收益最高( 81 - 1) = 80
20957

210-
### 题目描述
211-
212-
给定一个数组,它的第 *i* 个元素是一支给定的股票在第 *i* 天的价格。
213-
214-
设计一个算法来计算你所能获取的最大利润。你最多可以完成 *两笔* 交易。
215-
216-
**注意:** 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
217-
218-
**示例 1:**
219-
220-
```
221-
输入: [3,3,5,0,0,3,1,4]
222-
输出: 6
223-
解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
224-
随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
225-
```
226-
227-
**示例 2:**
228-
229-
```
230-
输入: [1,2,3,4,5]
231-
输出: 4
232-
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
233-
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
234-
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
235-
```
236-
237-
**示例 3:**
238-
239-
```
240-
输入: [7,6,4,3,1]
241-
输出: 0
242-
解释: 在这个情况下, 没有交易完成, 所以最大利润为 0。
243-
```
244-
245-
### 题目解析
246-
247-
这里限制了最多两笔交易。
58+
那为什么可以知道在第四天卖而不在第三天卖呢?
24859

249-
#### 状态
60+
实际上,注意题目是没有限制买卖交易次数的,完全可以在第三天卖出去,只不过发现第四天有涨了,那么就在第三天再买回来。
25061

251-
**第一次买入(fstBuy)****第一次卖出(fstSell)****第二次买入(secBuy)****第二次卖出(secSell)** 这四种状态
62+
` (81 - 1) = [( 20 - 1 ) + ( 81 - 20 )]`
25263

253-
#### 转移方程
64+
也就是说,第二天买、第三天卖,第三天买、第四天卖这四个动作与第二天买、第四天卖结果是一致的。
25465

255-
这里最多两次买入和两次卖出,也就是说 **买入** 状态之前可拥有 **卖出** 状态,**卖出** 状态之前可拥有 **买入** 状态,所以买入和卖出的转移方程都需要变化。
66+
**所以只需要今天的价格比昨天更高,就卖出**!(反正可以再买入)
25667

257-
- fstBuy = max(fstBuy , -price[i])
258-
- fstSell = max(fstSell,fstBuy + prices[i] )
259-
- secBuy = max(secBuy ,fstSell -price[i]) (受第一次卖出状态的影响)
260-
- secSell = max(secSell ,secBuy + prices[i] )
68+
总结一下就是:从第二天开始观察,如果当前价格(今天)比之前价格(昨天)高,则把差值加入到利润中(因为我们可以昨天买入,今天卖出,如果明天价位更高的话,还可以今天买入,明天再抛出)。以此类推,遍历完整个数组后即可求得最大利润。
26169

262-
#### 边界
70+
### 图片描述
26371

264-
- 一开始 `fstBuy = -prices[0]`
265-
- 买入后直接卖出,`fstSell = 0`
266-
- 买入后再卖出再买入,`secBuy - prices[0]`
267-
- 买入后再卖出再买入再卖出,`secSell = 0`
268-
269-
最后返回 secSell 。
72+
![买卖股票的最佳时机 II](https://upload-images.jianshu.io/upload_images/1940317-07904ca85dc535a9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
27073

27174
### 代码实现
27275

27376
```java
77+
//程序员小吴
27478
class Solution {
27579
public int maxProfit(int[] prices) {
276-
int fstBuy = Integer.MIN_VALUE, fstSell = 0;
277-
int secBuy = Integer.MIN_VALUE, secSell = 0;
278-
for(int i = 0; i < prices.length; i++) {
279-
fstBuy = Math.max(fstBuy, -prices[i]);
280-
fstSell = Math.max(fstSell, fstBuy + prices[i]);
281-
secBuy = Math.max(secBuy, fstSell - prices[i]);
282-
secSell = Math.max(secSell, secBuy + prices[i]);
80+
int profit = 0;
81+
for (int i = 1 ; i < prices.length; i++){
82+
if (prices[i] > prices[i-1]){
83+
profit = profit + prices[i] - prices[i - 1];
84+
}
28385
}
284-
return secSell;
285-
86+
return profit;
87+
28688
}
28789
}
28890
```
28991

29092

29193

94+
95+
29296
![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz0rq.png)
29397

29498

0 commit comments

Comments
 (0)