Tensorum
LeetCode 309 · State Machine / Stocks

Best Time to Buy and Sell Stock with Cooldown

LeetCode 309 · Medium · solution, intuition & step-by-step walkthrough

dfs(i, can_buy): selling moves to i+2 — that skipped day is the cooldown.

MediumArrayDynamic Programming
7 msruntime · beats 34%
24.6 MBmemory · beats 9%
1approach
5accepted runs · last 9 Apr 2026

This is how I solved Best Time to Buy and Sell Stock with Cooldown (LeetCode 309), a medium problem from the State Machine / Stocks pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Each day you are in one of two modes: free to buy, or holding a share. In buy mode you either buy or wait; in hold mode you either sell or wait. Selling forces you to skip the next day, which is just a jump of two days in the recursion.

Approach

  1. 1

    State: (index, can_buy) → best profit from this day onward.

  2. 2

    If can_buy: max(−price + dfs(i+1, holding), dfs(i+1, can_buy)).

  3. 3

    If holding: max(+price + dfs(i+2, can_buy), dfs(i+1, holding)).

  4. 4

    Base case: past the last day the profit is 0.

  5. 5

    lru_cache makes the 2n states each solve once.

How I solved it

State machine + memoPythonO(n) time · O(n) space
from functools import lru_cache
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        #two choices
        #buy, sell
        #buy/cool down
        #sell/cool down
        #if sell we need to wait for a day
        n = len(prices)
        @lru_cache(maxsize=None)
        def dfs(index,can_buy):
            if index>=n:
                return 0
            if can_buy:
                buy = -prices[index]+dfs(index+1,False)
                cooldown= dfs(index+1,True)
                return max(buy,cooldown)
            else:
                sell = prices[index]+dfs(index+2,True)
                cooldown= dfs(index+1,False)
                return max(sell,cooldown)
        return dfs(0,True)
Loading the step-through…

Best Time to Buy and Sell Stock with Cooldown — FAQ

What is the intuition behind Best Time to Buy and Sell Stock with Cooldown?

Each day you are in one of two modes: free to buy, or holding a share. In buy mode you either buy or wait; in hold mode you either sell or wait. Selling forces you to skip the next day, which is just a jump of two days in the recursion.

How do you solve Best Time to Buy and Sell Stock with Cooldown (LeetCode 309)?

1. State: (index, can_buy) → best profit from this day onward. 2. If can_buy: max(−price + dfs(i+1, holding), dfs(i+1, can_buy)). 3. If holding: max(+price + dfs(i+2, can_buy), dfs(i+1, holding)). 4. Base case: past the last day the profit is 0. 5. lru_cache makes the 2n states each solve once.

What is the time and space complexity of Best Time to Buy and Sell Stock with Cooldown?

Time: O(n). Space: O(n). (Accepted approach: State machine + memo.)

What are common mistakes in Best Time to Buy and Sell Stock with Cooldown?

The cooldown applies after a sell, not after a buy. Index can jump past n by two — the base case must use ≥ n.