Skip to content

Commit ed25885

Browse files
authored
Update 0121.买卖股票的最佳时机.md
增加了python 动态规划:版本三
1 parent bf2f215 commit ed25885

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ class Solution:
310310
return dp[(length-1) % 2][1]
311311
```
312312

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+
313325
Go:
314326
> 贪心法:
315327
```Go

0 commit comments

Comments
 (0)