We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bf2f215 commit ed25885Copy full SHA for ed25885
problems/0121.买卖股票的最佳时机.md
@@ -310,6 +310,18 @@ class Solution:
310
return dp[(length-1) % 2][1]
311
```
312
313
+> 动态规划:版本三
314
+```python
315
+class Solution:
316
+ def maxProfit(self, prices: List[int]) -> int:
317
+ length = len(prices)
318
+ dp0, dp1 = -prices[0], 0 #注意这里只维护两个常量,因为dp0的更新不受dp1的影响
319
+ for i in range(1, length):
320
+ dp1 = max(dp1, dp0 + prices[i])
321
+ dp0 = max(dp0, -prices[i])
322
+ return dp1
323
+```
324
+
325
Go:
326
> 贪心法:
327
```Go
0 commit comments