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

Commit3d41c31

Browse files
Merge pull requestyoungyangyang04#484 from jojoo15/patch-38
增加 0188 买股票的最佳时机IV python3版本二
2 parentsdafd104 +77e7503 commit3d41c31

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
@@ -224,7 +224,7 @@ class Solution {
224224

225225

226226
Python:
227-
227+
版本一
228228
```python
229229
classSolution:
230230
defmaxProfit(self,k:int,prices: List[int]) ->int:
@@ -239,7 +239,22 @@ class Solution:
239239
dp[i][j+2]=max(dp[i-1][j+2], dp[i-1][j+1]+ prices[i])
240240
return dp[-1][2*k]
241241
```
242-
242+
版本二
243+
```python3
244+
classSolution:
245+
defmaxProfit(self,k:int,prices: List[int]) ->int:
246+
iflen(prices)==0:return0
247+
dp= [0]* (2*k+1)
248+
for iinrange(1,2*k,2):
249+
dp[i]=-prices[0]
250+
for iinrange(1,len(prices)):
251+
for jinrange(1,2*k+1):
252+
if j%2:
253+
dp[j]=max(dp[j],dp[j-1]-prices[i])
254+
else:
255+
dp[j]=max(dp[j],dp[j-1]+prices[i])
256+
return dp[2*k]
257+
```
243258
Go:
244259

245260

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp