Skip to content

Commit aae2b25

Browse files
committed
122
1 parent 353d43f commit aae2b25

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

SUMMARY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
* [99. Recover Binary Search Tree](leetcode-99-Recover-Binary-Search-Tree.md)
103103
* [100. Same Tree](leetcode-100-Same-Tree.md)
104104
* [leetcode 100 斩!回顾](leetcode100斩回顾.md)
105-
* [101 题到 121](leetcode-101-200.md)
105+
* [101 题到 122](leetcode-101-200.md)
106106
* [101. Symmetric Tree](leetcode-101-Symmetric-Tree.md)
107107
* [102. Binary Tree Level Order Traversal](leetcode-102-Binary-Tree-Level-Order-Traversal.md)
108108
* [103. Binary Tree Zigzag Level Order Traversal](leetcode-103-Binary-Tree-Zigzag-Level-Order-Traversal.md)
@@ -123,4 +123,5 @@
123123
* [118. Pascal's Triangle](leetcode-118-Pascal's-Triangle.md)
124124
* [119. Pascal's Triangle II](leetcode-119-Pascal's-TriangleII.md)
125125
* [120. Triangle](leetcode-120-Triangle.md)
126-
* [121. Best Time to Buy and Sell Stock](leetcode-121-Best-Time-to-Buy-and-Sell-Stock.md)
126+
* [121. Best Time to Buy and Sell Stock](leetcode-121-Best-Time-to-Buy-and-Sell-Stock.md)
127+
* [122. Best Time to Buy and Sell Stock II](leetcode-122-Best-Time-to-Buy-and-Sell-StockII.md)

leetcode-101-200.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@
3838

3939
<a href="leetcode-120-Triangle.html">120. Triangle</a>
4040

41-
<a href="leetcode-121-Best-Time-to-Buy-and-Sell-Stock.html">121. Best Time to Buy and Sell Stock</a>
41+
<a href="leetcode-121-Best-Time-to-Buy-and-Sell-Stock.html">121. Best Time to Buy and Sell Stock</a>
42+
43+
<a href="leetcode-122-Best-Time-to-Buy-and-Sell-StockII.html">122. Best Time to Buy and Sell Stock II</a>

leetcode-121-Best-Time-to-Buy-and-Sell-Stock.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ public int maxProfit(int[] prices) {
9595

9696
# 解法三
9797

98-
参考 <a href = "<https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/39038/Kadane's-Algorithm-Since-no-one-has-mentioned-about-this-so-far-%3A)-(In-case-if-interviewer-twists-the-input)>">这里</a> ,一个很新的角度
98+
参考下边的链接
9999

100-
先回忆一下 [53 题](<https://leetcode.wang/leetCode-53-Maximum-Subarray.html>),求子序列最大的和。
100+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/39038/Kadane's-Algorithm-Since-no-one-has-mentioned-about-this-so-far-%3A)-(In-case-if-interviewer-twists-the-input)
101+
102+
一个很新的角度,先回忆一下 [53 题](<https://leetcode.wang/leetCode-53-Maximum-Subarray.html>),求子序列最大的和。
101103

102104
![img](https://windliang.oss-cn-beijing.aliyuncs.com/53.jpg)
103105

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 题目描述(简单难度)
2+
3+
![](https://windliang.oss-cn-beijing.aliyuncs.com/122.jpg)
4+
5+
[121 题](<https://leetcode.wang/leetcode-121-Best-Time-to-Buy-and-Sell-Stock.html>) 一样,给定一个数组,代表每天的价格。区别在于 `121` 题只能进行一次买入卖出。但是这道题可以不停的买入、卖出,但是只有卖出了才能继续买入。
6+
7+
# 解法一
8+
9+
就用最简单的思想,我们穿越回去了过去,知道了未来每天的股票价格,要怎么操作呢?
10+
11+
跌了的前一天卖出,例如下边的例子
12+
13+
```java
14+
1 2 3 4
15+
2 7 8 5
16+
17+
4 天下跌,我们可以在前一天卖出,下跌当天再次买入,后边出现下跌,前一天继续卖出
18+
```
19+
20+
需要考虑两种特殊情况
21+
22+
一直上涨,没有下跌
23+
24+
```java
25+
1 3 5 9
26+
27+
那么我们在最后一天卖出就可以
28+
```
29+
30+
第二天下跌
31+
32+
```java
33+
8 7 9 10
34+
35+
下跌的时候我们本应该在前一天卖出,然而第一天只能买入并不能卖出,所以这种情况并不会带来收益
36+
```
37+
38+
考虑了上边的所有情况,就可以写代码了。
39+
40+
```java
41+
public int maxProfit(int[] prices) {
42+
int profit = 0;
43+
int buy = 0;
44+
int sell = 1;
45+
46+
for (; sell < prices.length; sell++) {
47+
//出现下跌
48+
if (prices[sell] < prices[sell - 1]) {
49+
//不是第 2 天下跌,就前一天卖出,累计收益
50+
if (sell != 1) {
51+
profit += prices[sell - 1] - prices[buy];
52+
}
53+
//下跌当天再次买入
54+
buy = sell;
55+
56+
//到最后一天是上涨,那就在最后一天卖出
57+
} else if (sell == prices.length - 1) {
58+
profit += prices[sell] - prices[buy];
59+
}
60+
}
61+
return profit;
62+
}
63+
```
64+
65+
还有一种持续下跌的情况
66+
67+
```java
68+
9 8 7 3 2
69+
70+
但是对于我们的代码,持续下跌的话,buy 和 sell - 1 就相等了,所以每次累计就是 0,不影响结果
71+
```
72+
73+
# 解法二
74+
75+
其实不用考虑那么多,再直接点,只要当前天相对于前一天上涨了,我们就前一天买入,当前天卖出。
76+
77+
```java
78+
public int maxProfit(int[] prices) {
79+
int profit = 0;
80+
for (int i = 1; i < prices.length; i++) {
81+
int sub = prices[i] - prices[i - 1];
82+
if (sub > 0) {
83+
profit += sub;
84+
}
85+
}
86+
return profit;
87+
}
88+
```
89+
90+
#
91+
92+
上边两种解法都是从实际情况出发,来考虑怎么盈利最大。[官方](<https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/>) 给出的理解方式也很好,这里分享一下。
93+
94+
![](https://windliang.oss-cn-beijing.aliyuncs.com/122_2.jpg)
95+
96+
两种解法其实都可以抽象到上边的图中。
97+
98+
解法一,其实每次就是找了波谷和波峰做了差,然后把所有的差进行累计。
99+
100+
解法二,找的是上升的折线段,把所有上升的折线段的高度进行了累计。
101+
102+
所以一些题,可能代码是一样的,但是理解的含义并不相同。

0 commit comments

Comments
 (0)