2023-08-09 12:13:28 +0000 UTC
Best Time to Buy and Sell Stock IV
Categories:
Links
Code
class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
# no transaction, no profit
if k == 0: return 0
# dp[k][0] = min cost you need to spend at most k transactions
# dp[k][1] = max profit you can achieve at most k transactions
dp = [[1000, 0] for _ in range(k + 1)]
for price in prices:
for i in range(1, k + 1):
# price - dp[i - 1][1] is how much you need to spend
# i.e use the profit you earned from previous transaction to buy the stock
# we want to minimize it
dp[i][0] = min(dp[i][0], price - dp[i - 1][1])
# price - dp[i][0] is how much you can achieve from previous min cost
# we want to maximize it
dp[i][1] = max(dp[i][1], price - dp[i][0])
# return max profit at most k transactions
# or you can write `return dp[-1][1]`
return dp[k][1]