Skip to content

Commit 77e7503

Browse files
authored
增加 0188 买股票的最佳时机IV python3版本二
增加 0188 买股票的最佳时机IV python3版本二
1 parent a9344c2 commit 77e7503

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
class Solution:
217217
def maxProfit(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+
class Solution:
232+
def maxProfit(self, k: int, prices: List[int]) -> int:
233+
if len(prices) == 0: return 0
234+
dp = [0] * (2*k + 1)
235+
for i in range(1,2*k,2):
236+
dp[i] = -prices[0]
237+
for i in range(1,len(prices)):
238+
for j in range(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)