Skip to content

Commit

Permalink
Update 0121.买卖股票的最佳时机.md
Browse files Browse the repository at this point in the history
增加了python 动态规划:版本三
  • Loading branch information
roylx authored Nov 14, 2022
1 parent bf2f215 commit ed25885
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions problems/0121.买卖股票的最佳时机.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed25885

Please sign in to comment.