mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0188.买卖股票的最佳时机IV.md
This commit is contained in:
@ -212,6 +212,20 @@ class Solution { //动态规划
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, k: int, prices: List[int]) -> int:
|
||||
if len(prices) == 0:
|
||||
return 0
|
||||
dp = [[0] * (2*k+1) for _ in range(len(prices))]
|
||||
for j in range(1, 2*k, 2):
|
||||
dp[0][j] = -prices[0]
|
||||
for i in range(1, len(prices)):
|
||||
for j in range(0, 2*k-1, 2):
|
||||
dp[i][j+1] = max(dp[i-1][j+1], dp[i-1][j] - prices[i])
|
||||
dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i])
|
||||
return dp[-1][2*k]
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user