Skip to content

Commit

Permalink
增加 0188 买股票的最佳时机IV python3版本二
Browse files Browse the repository at this point in the history
增加 0188 买股票的最佳时机IV python3版本二
  • Loading branch information
jojoo15 committed Jul 9, 2021
1 parent a9344c2 commit 77e7503
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions problems/0188.买卖股票的最佳时机IV.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Solution { //动态规划


Python:

版本一
```python
class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
Expand All @@ -226,7 +226,22 @@ class Solution:
dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i])
return dp[-1][2*k]
```

版本二
```python3
class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
if len(prices) == 0: return 0
dp = [0] * (2*k + 1)
for i in range(1,2*k,2):
dp[i] = -prices[0]
for i in range(1,len(prices)):
for j in range(1,2*k + 1):
if j % 2:
dp[j] = max(dp[j],dp[j-1]-prices[i])
else:
dp[j] = max(dp[j],dp[j-1]+prices[i])
return dp[2*k]
```
Go:


Expand Down

0 comments on commit 77e7503

Please sign in to comment.