Algorithms · Visualized

DP: Stock with Cooldown

Dynamic Programming

Break problems into overlapping subproblems and store answers.

Maximizes profit from unlimited stock buy/sell transactions, but with a rule: after selling, you must wait one day (cooldown) before buying again. Time: O(n) average, O(n) worst; space: O(n), reducible to O(1) by keeping only the previous day's values.

Big-O: The most-asked interview category — 35 classics here.

Best Time to Buy and Sell Stock with Cooldown

Dynamic Programming

Maximizes profit from unlimited stock buy/sell transactions, but with a rule: after selling, you must wait one day (cooldown) before buying again. This is modeled with three states per day — 'holding a stock', 'just sold (in cooldown)', and 'free to buy (not holding, not in cooldown)' — and DP transitions track the best profit achievable in each state.

How Stock with Cooldown Works

  1. Define three states for each day: hold[i] (max profit while holding a stock on day i), sold[i] (max profit having just sold on day i), and rest[i] (max profit while free/resting on day i).
  2. Base cases on day 0: hold[0] = -prices[0] (bought the stock), sold[0] = 0 (can't have sold yet, treat as 0), rest[0] = 0.
  3. Transition for hold[i]: either you were already holding (hold[i-1]) or you just bought today from a resting state (rest[i-1] - prices[i]) — take the max.
  4. Transition for sold[i]: you must have been holding yesterday and sold today — sold[i] = hold[i-1] + prices[i].
  5. Transition for rest[i]: either you were already resting, or your cooldown from selling two days ago just ended — rest[i] = max(rest[i-1], sold[i-1]). The final answer is the maximum of sold[n-1] and rest[n-1] (you should never end while still holding).

Stock with Cooldown Worked Example

Input: [1, 2, 3, 0, 2]

Day0: hold=-1,sold=0,rest=0. Day1(price2): hold=max(-1,0-2)=-1, sold=hold_prev+2=-1+2=1, rest=max(0,0)=0. Day2(price3): hold=max(-1,0-3)=-1, sold=hold_prev+3=-1+3=2, rest=max(0,1)=1. Day3(price0): hold=max(-1,1-0)=1, sold=-1+0=-1, rest=max(1,2)=2. Day4(price2): hold=max(1,2-2)=1, sold=1+2=3, rest=max(2,-1)=2. Final answer: max(sold,rest) at last day = max(3,2)=3.

DP: Stock with CooldownO(n)
Three-state DP over buy/sell days.
1
0
2
1
3
2
0
3
2
4
Step 1 / 7
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Best Time to Buy/Sell with Cooldown: three states — rest (no stock), hold (owning), sold (just sold).
Python · Dynamic Programming Algorithms in Code
def max_profit(prices):
    hold = float('-inf')
    sold = rest = 0
    for p in prices:
        prev_sold = sold
        sold = hold + p
        hold = max(hold, rest - p)
        rest = max(rest, prev_sold)
    return max(sold, rest)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Stock with Cooldown Time & Space Complexity

BestO(n)
AverageO(n)
WorstO(n)
SpaceO(n), reducible to O(1) by keeping only the previous day's values

When to Use Stock with Cooldown

  • Modeling real trading strategies with enforced waiting periods (settlement delays, regulatory cooldowns) between transactions.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.