diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 63ac5d04..cf17f48d 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -310,6 +310,18 @@ class Solution: return dp[(length-1) % 2][1] ``` +> 动态规划:版本三 +```python +class Solution: + def maxProfit(self, prices: List[int]) -> int: + length = len(prices) + dp0, dp1 = -prices[0], 0 #注意这里只维护两个常量,因为dp0的更新不受dp1的影响 + for i in range(1, length): + dp1 = max(dp1, dp0 + prices[i]) + dp0 = max(dp0, -prices[i]) + return dp1 +``` + Go: > 贪心法: ```Go