Update 0121.买卖股票的最佳时机.md

增加了python 动态规划:版本三
This commit is contained in:
roylx
2022-11-14 15:00:07 -07:00
committed by GitHub
parent bf2f215601
commit ed2588520a

View File

@ -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