309. Best Time to Buy and Sell Stock with Cooldown

class Solution(object):
    def maxProfit(self, prices):
        dp = {}

        def dfs(i, buying):
            if i >= len(prices):
                return 0 
            if (i, buying) in dp:
                return dp[(i, buying)]

            cooldown = dfs(i + 1, buying)

            if buying:
                buy = dfs(i + 1, not buying) - prices[i]
                dp[(i, buying)] = max(buy, cooldown)
            else:
                sell = dfs(i + 2, not buying) + prices[i]
                dp[(i, buying)] = max(sell, cooldown)

            return dp[(i, buying)]

        return dfs(0, True)

CleanShot 2024-03-26 at 12.58.41.png