Best Time to Buy and Sell Stock with Cooldown

Solution Code
class Solution:
    def maxProfit(self, prices: list[int]) -> int:
        if not prices:
            return 0
        
        hold = -float('inf')
        sold = 0
        cooldown = 0
        
        for price in prices:
            prev_hold = hold
            hold = max(hold, cooldown - price)
            prev_sold = sold
            sold = prev_hold + price
            cooldown = max(cooldown, prev_sold)
            
        return max(sold, cooldown)
Video GuideLeetcode Daily

Time Complexity

O(N)

Space Complexity

O(1)