File tree Expand file tree Collapse file tree 4 files changed +112
-5
lines changed Expand file tree Collapse file tree 4 files changed +112
-5
lines changed Original file line number Diff line number Diff line change 102
102
* [ 99. Recover Binary Search Tree] ( leetcode-99-Recover-Binary-Search-Tree.md )
103
103
* [ 100. Same Tree] ( leetcode-100-Same-Tree.md )
104
104
* [ leetcode 100 斩!回顾] ( leetcode100斩回顾.md )
105
- * [ 101 题到 121 题] ( leetcode-101-200.md )
105
+ * [ 101 题到 122 题] ( leetcode-101-200.md )
106
106
* [ 101. Symmetric Tree] ( leetcode-101-Symmetric-Tree.md )
107
107
* [ 102. Binary Tree Level Order Traversal] ( leetcode-102-Binary-Tree-Level-Order-Traversal.md )
108
108
* [ 103. Binary Tree Zigzag Level Order Traversal] ( leetcode-103-Binary-Tree-Zigzag-Level-Order-Traversal.md )
123
123
* [ 118. Pascal's Triangle] ( leetcode-118-Pascal's-Triangle.md )
124
124
* [ 119. Pascal's Triangle II] ( leetcode-119-Pascal's-TriangleII.md )
125
125
* [ 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 )
Original file line number Diff line number Diff line change 38
38
39
39
<a href =" leetcode-120-Triangle.html " >120. Triangle</a >
40
40
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 >
Original file line number Diff line number Diff line change @@ -95,9 +95,11 @@ public int maxProfit(int[] prices) {
95
95
96
96
# 解法三
97
97
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
+ 参考下边的链接 。
99
99
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 > ) ,求子序列最大的和。
101
103
102
104
![ img] ( https://windliang.oss-cn-beijing.aliyuncs.com/53.jpg )
103
105
Original file line number Diff line number Diff line change
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
+ 所以一些题,可能代码是一样的,但是理解的含义并不相同。
You can’t perform that action at this time.
0 commit comments