mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
增加 0188 买股票的最佳时机IV python3版本二
增加 0188 买股票的最佳时机IV python3版本二
This commit is contained in:
@ -211,7 +211,7 @@ class Solution { //动态规划
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
版本一
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def maxProfit(self, k: int, prices: List[int]) -> int:
|
def maxProfit(self, k: int, prices: List[int]) -> int:
|
||||||
@ -226,7 +226,22 @@ class Solution:
|
|||||||
dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i])
|
dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i])
|
||||||
return dp[-1][2*k]
|
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:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user