LC309 - Best Time to Buy and Sell Stock with Cooldown
Last updated
Last updated
def maxProfit(self, prices: List[int]) -> int:
@cache
def dfs(i, cool):
# end
if i >= len(prices):
return 0
# cooldown
profit = dfs(i+1, cool)
# buy
if not cool:
profit = max(profit, dfs(i+1, True) - prices[i])
# sell
else:
profit = max(profit, dfs(i+2, False) + prices[i])
return profit
return dfs(0, False)