mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0121.买卖股票的最佳时机.md
增加了python 动态规划:版本三
This commit is contained in:
@ -310,6 +310,18 @@ class Solution:
|
|||||||
return dp[(length-1) % 2][1]
|
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:
|
||||||
> 贪心法:
|
> 贪心法:
|
||||||
```Go
|
```Go
|
||||||
|
Reference in New Issue
Block a user