Best Time to Buy and Sell Stock IV LeetCode solution in Python

1
class Solution:
2
def maxProfit(self, k: int, prices: List[int]) -> int:
3
buy = [-inf] * (k + 1)
4
sell = [0] * (k + 1)
5
for price in prices:
6
for i in range(1, k + 1):
7
buy[i] = max(buy[i], sell[i - 1] - price)
8
sell[i] = max(sell[i], buy[i] + price)
9
return sell[-1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0