Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit77e7503

Browse files
authored
增加 0188 买股票的最佳时机IV python3版本二
增加 0188 买股票的最佳时机IV python3版本二
1 parenta9344c2 commit77e7503

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

‎problems/0188.买卖股票的最佳时机IV.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class Solution { //动态规划
211211

212212

213213
Python:
214-
214+
版本一
215215
```python
216216
classSolution:
217217
defmaxProfit(self,k:int,prices: List[int]) ->int:
@@ -226,7 +226,22 @@ class Solution:
226226
dp[i][j+2]=max(dp[i-1][j+2], dp[i-1][j+1]+ prices[i])
227227
return dp[-1][2*k]
228228
```
229-
229+
版本二
230+
```python3
231+
classSolution:
232+
defmaxProfit(self,k:int,prices: List[int]) ->int:
233+
iflen(prices)==0:return0
234+
dp= [0]* (2*k+1)
235+
for iinrange(1,2*k,2):
236+
dp[i]=-prices[0]
237+
for iinrange(1,len(prices)):
238+
for jinrange(1,2*k+1):
239+
if j%2:
240+
dp[j]=max(dp[j],dp[j-1]-prices[i])
241+
else:
242+
dp[j]=max(dp[j],dp[j-1]+prices[i])
243+
return dp[2*k]
244+
```
230245
Go:
231246

232247

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp