|
21 | 21 | <strong>输出: </strong>3
|
22 | 22 | <strong>解释:</strong> 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]</pre>
|
23 | 23 |
|
24 |
| - |
25 | 24 | ## 解法
|
26 | 25 |
|
27 | 26 | <!-- 这里可写通用的实现逻辑 -->
|
28 | 27 |
|
| 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 | + |
29 | 42 | <!-- tabs:start -->
|
30 | 43 |
|
31 | 44 | ### **Python3**
|
32 | 45 |
|
33 | 46 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
34 | 47 |
|
35 | 48 | ```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 |
37 | 61 | ```
|
38 | 62 |
|
39 | 63 | ### **Java**
|
40 | 64 |
|
41 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
42 | 66 |
|
43 | 67 | ```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 | +``` |
44 | 104 |
|
| 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 | +} |
45 | 126 | ```
|
46 | 127 |
|
47 | 128 | ### **...**
|
|
0 commit comments