Browse curriculum

Best Time to Buy and Sell Stock with Cooldown

Maximise stock profit when every sale is followed by a one-day cooldown, with three DP states per day: hold, sold and rest.

Problem Understanding

Best Time to Buy and Sell Stock with Cooldown: prices[i] is the price on day i. You may complete as many trades as you like, holding at most one share at a time, but after you sell you cannot buy on the next day. Return the maximum profit.

Example: prices = [1, 2, 3, 0, 2] returns 3: buy, sell, cooldown, buy, sell.

The Intuition: Three States, One Day at a Time

At the end of any day you are in exactly one situation, and the rules only care about that:

  1. hold: you own a share. You got here by holding yesterday, or by buying today, which is only allowed from rest.
  2. sold: you sold today. You got here only from hold, earning today's price.
  3. rest: you own nothing and are free. You got here by resting yesterday, or because yesterday you sold and today is the cooldown.

Keep the best profit for each state and update all three from yesterday's values. That is a small state machine, a classic shape of dynamic programming on sequences, and it needs only three variables. Without the cooldown, Stock II is solved greedily; with one trade only, Stock I tracks the lowest price so far.

Interactive Walkthrough

Each column is a day; the rows hold the best profit for hold, sold and rest. The day being computed is solid and the column it reads is tinted. At the end the best path turns green with its buys and sells. Try Zigzag to see the cooldown cost a trade.

Dry Run Table

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

Day Price hold sold rest
0 1 -1 0 0
1 2 -1 1 0
2 3 -1 2 1
3 0 1 -1 2
4 2 1 3 2
  • Day 3: hold = max(-1, rest 1 - 0) = 1: buy at 0 after the day-2 cooldown.
  • Day 4: sold = 1 + 2 = 3.

Answer: max(sold 3, rest 2) = 3: buy 1, sell 2, cooldown, buy 0, sell 2.

The Solution Template

Three rolling variables. prevSold keeps yesterday's sold, because rest needs it after sold is overwritten.

Best Time to Buy and Sell Stock with Cooldown Code
1var maxProfit = function(prices) {
2 let hold = -prices[0], sold = 0, rest = 0;
3 for (let i = 1; i < prices.length; i++) {
4 const prevSold = sold;
5 sold = hold + prices[i];
6 hold = Math.max(hold, rest - prices[i]);
7 rest = Math.max(rest, prevSold);
8 }
9 return Math.max(sold, rest);
10};

Edge Cases & Common Mistakes

  • One day: no sale is possible; the answer is 0.
  • Falling prices: never buy; rest stays 0.
  • Updating in the wrong order: rest needs yesterday's sold and hold needs yesterday's rest; save prevSold or use temporaries.
  • Buying from sold: that is buying the day after a sale, which the cooldown forbids. Buy only from rest.
  • Returning hold: ending with a share you never sold can never beat max(sold, rest).

Summary

Track three states per day: hold, sold and rest. sold = hold + price, hold = max(hold, rest - price), rest = max(rest, previous sold). Return max(sold, rest). O(n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start on day 0

    hold = -prices[0], sold = 0, rest = 0.

  2. Update the three states each day

    Using yesterday's values: sold = hold + price, hold = max(hold, rest - price), rest = max(rest, sold).

  3. Return the best empty-handed state

    Return max(sold, rest) on the last day; ending in hold means paying for a share you never sell.

Frequently asked questions

What are the DP states for Stock with Cooldown?

Three, each the best profit at the end of a day: hold (you own a share), sold (you sold today, so tomorrow is a cooldown) and rest (you own nothing and may buy). sold = hold + price, hold = max(hold, rest - price), rest = max(rest, yesterday's sold).


Why can you only buy from the rest state?

Buying from sold would mean buying the day right after a sale, which the cooldown forbids. A day spent in rest after selling is the cooldown; only after it can rest - price start a new hold.


How is this different from Best Time to Buy and Sell Stock II?

Stock II allows unlimited trades with no waiting, so a greedy sum of every rise is optimal. The cooldown makes back-to-back trades cost a day, so sometimes it is better to skip a small rise or hold through a dip. That trade-off needs DP.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description