Skip to content

Commit 3badf26

Browse files
committed
feat: add solutions to lc problem: No.0309. Best Time to Buy and Sell Stock with Cooldown
1 parent c6f8f58 commit 3badf26

File tree

8 files changed

+205
-18
lines changed

8 files changed

+205
-18
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
- [最佳观光组合](./solution/1000-1099/1014.Best%20Sightseeing%20Pair/README.md)
175175
- [买卖股票的最佳时机](./solution/0100-0199/0121.Best%20Time%20to%20Buy%20and%20Sell%20Stock/README.md)
176176
- [买卖股票的最佳时机 II](./solution/0100-0199/0122.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/README.md)
177+
- [最佳买卖股票时机含冷冻期](./solution/0300-0399/0309.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown/README.md)
177178
- [接雨水](./solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
178179
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
179180
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
168168
- [Best Sightseeing Pair](./solution/1000-1099/1014.Best%20Sightseeing%20Pair/README_EN.md)
169169
- [Best Time to Buy and Sell Stock](./solution/0100-0199/0121.Best%20Time%20to%20Buy%20and%20Sell%20Stock/README_EN.md)
170170
- [Best Time to Buy and Sell Stock II](./solution/0100-0199/0122.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/README_EN.md)
171+
- [Best Time to Buy and Sell Stock with Cooldown](./solution/0300-0399/0309.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown/README_EN.md)
171172
- [Trapping Rain Water](./solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
172173
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
173174
- [Decode Ways](./solution/0000-0099/0091.Decode%20Ways/README_EN.md)

solution/0300-0399/0309.Best Time to Buy and Sell Stock with Cooldown/README.md

+83-2
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,108 @@
2121
<strong>输出: </strong>3
2222
<strong>解释:</strong> 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]</pre>
2323

24-
2524
## 解法
2625

2726
<!-- 这里可写通用的实现逻辑 -->
2827

28+
动态规划法。
29+
30+
设 f1 表示当天买入股票后的最大利润,f2 表示当天卖出股票后的最大利润,f3 表示当天空仓后的最大利润。
31+
32+
初始第 1 天结束时,`f1 = -prices[0]``f2 = 0``f3 = 0`
33+
34+
从第 2 天开始,当天结束时:
35+
36+
- 若买入,则说明前一天空仓,然后今天买入,`f1 = max(f1, f3 - prices[i])`
37+
- 若卖出,则只能是之前某一天买入,然后今天卖出,`f2 = max(f2, f1 + prices[i])`
38+
- 若空仓,则只能是之前某一天卖出后,然后今天保持空仓,`f3 = max(f3, f2)`
39+
40+
最后返回 f2 即可。
41+
2942
<!-- tabs:start -->
3043

3144
### **Python3**
3245

3346
<!-- 这里可写当前语言的特殊实现逻辑 -->
3447

3548
```python
36-
49+
class Solution:
50+
def maxProfit(self, prices: List[int]) -> int:
51+
# 买入,卖出,继续空仓
52+
f1, f2, f3 = -prices[0], 0, 0
53+
res = f2
54+
for price in prices[1:]:
55+
pf1, pf2, pf3 = f1, f2, f3
56+
f1 = max(pf1, pf3 - price)
57+
f2 = max(pf2, pf1 + price)
58+
f3 = max(pf3, pf2)
59+
res = max(res, f2)
60+
return res
3761
```
3862

3963
### **Java**
4064

4165
<!-- 这里可写当前语言的特殊实现逻辑 -->
4266

4367
```java
68+
class Solution {
69+
public int maxProfit(int[] prices) {
70+
// 买入,卖出,继续空仓
71+
int f1 = -prices[0], f2 = 0, f3 = 0;
72+
int res = f2;
73+
for (int i = 1; i < prices.length; ++i) {
74+
int pf1 = f1, pf2 = f2, pf3 = f3;
75+
f1 = Math.max(pf1, pf3 - prices[i]);
76+
f2 = Math.max(pf2, pf1 + prices[i]);
77+
f3 = Math.max(pf3, pf2);
78+
res = Math.max(res, f2);
79+
}
80+
return res;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int maxProfit(vector<int>& prices) {
91+
int f1 = -prices[0], f2 = 0, f3 = 0;
92+
int res = f2;
93+
for (int i = 1; i < prices.size(); ++i) {
94+
int pf1 = f1, pf2 = f2, pf3 = f3;
95+
f1 = max(pf1, pf3 - prices[i]);
96+
f2 = max(pf2, pf1 + prices[i]);
97+
f3 = max(pf3, pf2);
98+
res = max(res, f2);
99+
}
100+
return res;
101+
}
102+
};
103+
```
44104
105+
### **Go**
106+
107+
```go
108+
func maxProfit(prices []int) int {
109+
f1, f2, f3, res := -prices[0], 0, 0, 0
110+
for i := 1; i < len(prices); i++ {
111+
pf1, pf2, pf3 := f1, f2, f3
112+
f1 = max(pf1, pf3-prices[i])
113+
f2 = max(pf2, pf1+prices[i])
114+
f3 = max(pf3, pf2)
115+
res = max(res, f2)
116+
}
117+
return res
118+
}
119+
120+
func max(a, b int) int {
121+
if a > b {
122+
return a
123+
}
124+
return b
125+
}
45126
```
46127

47128
### **...**

solution/0300-0399/0309.Best Time to Buy and Sell Stock with Cooldown/README_EN.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,79 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maxProfit(self, prices: List[int]) -> int:
51+
f1, f2, f3 = -prices[0], 0, 0
52+
res = f2
53+
for price in prices[1:]:
54+
pf1, pf2, pf3 = f1, f2, f3
55+
f1 = max(pf1, pf3 - price)
56+
f2 = max(pf2, pf1 + price)
57+
f3 = max(pf3, pf2)
58+
res = max(res, f2)
59+
return res
5060
```
5161

5262
### **Java**
5363

5464
```java
65+
class Solution {
66+
public int maxProfit(int[] prices) {
67+
int f1 = -prices[0], f2 = 0, f3 = 0;
68+
int res = f2;
69+
for (int i = 1; i < prices.length; ++i) {
70+
int pf1 = f1, pf2 = f2, pf3 = f3;
71+
f1 = Math.max(pf1, pf3 - prices[i]);
72+
f2 = Math.max(pf2, pf1 + prices[i]);
73+
f3 = Math.max(pf3, pf2);
74+
res = Math.max(res, f2);
75+
}
76+
return res;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int maxProfit(vector<int>& prices) {
87+
int f1 = -prices[0], f2 = 0, f3 = 0;
88+
int res = f2;
89+
for (int i = 1; i < prices.size(); ++i) {
90+
int pf1 = f1, pf2 = f2, pf3 = f3;
91+
f1 = max(pf1, pf3 - prices[i]);
92+
f2 = max(pf2, pf1 + prices[i]);
93+
f3 = max(pf3, pf2);
94+
res = max(res, f2);
95+
}
96+
return res;
97+
}
98+
};
99+
```
55100
101+
### **Go**
102+
103+
```go
104+
func maxProfit(prices []int) int {
105+
f1, f2, f3, res := -prices[0], 0, 0, 0
106+
for i := 1; i < len(prices); i++ {
107+
pf1, pf2, pf3 := f1, f2, f3
108+
f1 = max(pf1, pf3-prices[i])
109+
f2 = max(pf2, pf1+prices[i])
110+
f3 = max(pf3, pf2)
111+
res = max(res, f2)
112+
}
113+
return res
114+
}
115+
116+
func max(a, b int) int {
117+
if a > b {
118+
return a
119+
}
120+
return b
121+
}
56122
```
57123

58124
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int f1 = -prices[0], f2 = 0, f3 = 0;
5+
int res = f2;
6+
for (int i = 1; i < prices.size(); ++i) {
7+
int pf1 = f1, pf2 = f2, pf3 = f3;
8+
f1 = max(pf1, pf3 - prices[i]);
9+
f2 = max(pf2, pf1 + prices[i]);
10+
f3 = max(pf3, pf2);
11+
res = max(res, f2);
12+
}
13+
return res;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func maxProfit(prices []int) int {
2+
f1, f2, f3, res := -prices[0], 0, 0, 0
3+
for i := 1; i < len(prices); i++ {
4+
pf1, pf2, pf3 := f1, f2, f3
5+
f1 = max(pf1, pf3-prices[i])
6+
f2 = max(pf2, pf1+prices[i])
7+
f3 = max(pf3, pf2)
8+
res = max(res, f2)
9+
}
10+
return res
11+
}
12+
13+
func max(a, b int) int {
14+
if a > b {
15+
return a
16+
}
17+
return b
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
class Solution {
22
public int maxProfit(int[] prices) {
3-
if(prices == null || prices.length == 0) return 0;
4-
5-
int n = prices.length;
6-
// buy[i] 表示第i天持有股票,最大利润
7-
int[] buy = new int[n];
8-
// sell[i] 表示第i天为持股,最大利润
9-
int[] sell = new int[n];
10-
buy[0] = -prices[0];
11-
sell[0] = 0;
12-
13-
for(int i = 1; i < n; i++) {
14-
buy[i] = Math.max(buy[i - 1], (i > 1 ? sell[i - 2] : 0) - prices[i]);
15-
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);
3+
int f1 = -prices[0], f2 = 0, f3 = 0;
4+
int res = f2;
5+
for (int i = 1; i < prices.length; ++i) {
6+
int pf1 = f1, pf2 = f2, pf3 = f3;
7+
f1 = Math.max(pf1, pf3 - prices[i]);
8+
f2 = Math.max(pf2, pf1 + prices[i]);
9+
f3 = Math.max(pf3, pf2);
10+
res = Math.max(res, f2);
1611
}
17-
18-
return sell[n - 1];
12+
return res;
1913
}
2014
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
f1, f2, f3 = -prices[0], 0, 0
4+
res = f2
5+
for price in prices[1:]:
6+
pf1, pf2, pf3 = f1, f2, f3
7+
f1 = max(pf1, pf3 - price)
8+
f2 = max(pf2, pf1 + price)
9+
f3 = max(pf3, pf2)
10+
res = max(res, f2)
11+
return res

0 commit comments

Comments
 (0)