题目:
题解:
class Solution:def maxProfit(self, k: int, prices: List[int]) -> int:if not prices:return 0n = len(prices)k = min(k, n // 2)buy = [0] * (k + 1)sell = [0] * (k + 1)buy[0], sell[0] = -prices[0], 0for i in range(1, k + 1):buy[i] = sell[i] = float("-inf")for i in range(1, n):buy[0] = max(buy[0], sell[0] - prices[i])for j in range(1, k + 1):buy[j] = max(buy[j], sell[j] - prices[i])sell[j] = max(sell[j], buy[j - 1] + prices[i]); return max(sell)