Best Time to Buy and Sell Stock with Cooldown
Problem
You are given an array prices where prices[i] is the price of a share on day i. You may complete as many buy-then-sell transactions as you want, holding at most one share at a time, with one restriction: after selling a share you must wait one full day before buying again (you cannot buy on the day immediately after a sale). Return the maximum total profit.
pricesholds up to a few thousand daily prices.- Each price is a non-negative integer, at most a few thousand.
Examples
prices = [2, 4, 9, 1, 5]→7. Buy at 2 on day 0, sell at 9 on day 2; selling at 4 first would force a cooldown that forfeits the run up to 9.prices = [4]→0. Nothing to sell to.prices = [5, 2]→0. Falling prices, no profitable trade.
Hints
- Each day you are in one of a small number of situations: holding a share, just sold, or free to buy. Draw those as states and ask what actions move between them.
- For each state, the best balance today depends only on the best balances of the states you could have come from yesterday.
- Three-state DP:
hold,sold(must rest today), andrest(free to buy). The cooldown is enforced by only allowing buys fromrest, andsoldfeeds intoresta day later.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.