Best Time to Buy and Sell Stock with Cooldown

Solve this Problem
Medium20–25 min
Topics
Companies
Practice:LeetCode ↗
This is the unlimited-trades stock problem with one extra rule: right after selling, you must skip a full day before you're allowed to buy again — a mandatory cooldown. Without the cooldown, tracking whether you're currently holding a share is enough state to make every decision. The cooldown breaks that, because "just sold" and "free to buy" are no longer the same situation — a share sold yesterday can't be replaced today no matter how attractive the price looks. Splitting "not holding" into two separate states — just sold (cooldown pending) and resting (free to act) — restores a clean one-pass update: each day's hold, sold, and resting totals depend only on yesterday's three totals, with the cooldown rule baked directly into which of yesterday's states feeds into today's hold.

Test Case 1:

Input:prices = [1,2,3,0,2]
Output:3
Explanation:Buy on day 0 (price 1), sell on day 1 (price 2) for +1. Day 2 is a forced cooldown right after that sale. Buy again on day 3 (price 0), sell on day 4 (price 2) for +2. Total: 1 + 2 = 3.

Test Case 2:

Input:prices = [1]
Output:0
Explanation:A single day is never enough to complete a buy-then-sell — there's nothing to do.

Test Case 3:

Input:prices = [7,6,4,3,1]
Output:0
Explanation:Prices only fall, so every buy would be followed only by cheaper sell days. Best to never trade.

Constraints

  • 1 ≤ prices.length ≤ 12
  • 0 ≤ prices[i] ≤ 1000
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Recursive Without Memoization

Brute

Extend the plain unlimited-trades recursion with a third piece of state: whether today is a forced cooldown day. A cooldown day has exactly one legal move — sit out and move on — so it collapses back to the two-way "do nothing or act" branch on the very next day. The moment a sale happens, the following day is marked as a cooldown before recursing. As with the plain version, the same (day, holding, cooldown) situation gets rediscovered from many different pasts, so trying every branch without remembering past answers costs roughly double the work per extra day.

TimeO(2ⁿ)
SpaceO(n)
1class Solution { 2 private int[] prices; 3 4 public int maxProfit(int[] prices) { 5 this.prices = prices; 6 return solve(0, false, false); 7 } 8 9 private int solve(int day, boolean holding, boolean cooldown) { 10 if (day >= prices.length) return 0; 11 if (cooldown) return solve(day + 1, holding, false); 12 int doNothing = solve(day + 1, holding, false); 13 int act; 14 if (holding) { 15 act = prices[day] + solve(day + 1, false, true); 16 } else { 17 act = -prices[day] + solve(day + 1, true, false); 18 } 19 return Math.max(doNothing, act); 20 } 21}

Optimal — Three-State Bottom-Up DP

Optimal

Instead of exploring both branches at every day, track the best profit reachable in exactly three situations after each day: `hold` (currently own a share), `sold` (just sold today, so tomorrow is forced to be a cooldown), and `rest` (own nothing and free to buy). Each day, `hold` either keeps yesterday's held position or buys fresh using yesterday's `rest` balance; `sold` always comes from yesterday's `hold` plus today's price; and `rest` carries forward the better of yesterday's `rest` or `sold`, since a completed sale always clears its cooldown by the day after next. The answer is whichever of `sold` or `rest` is larger once every day is processed — never `hold`, since ending the whole sequence still holding a share is by definition worse than having sold it at some point.

TimeO(n)
SpaceO(1)
1class Solution { 2 public int maxProfit(int[] prices) { 3 int n = prices.length; 4 if (n == 0) return 0; 5 int hold = -prices[0], sold = 0, rest = 0; 6 for (int i = 1; i < n; i++) { 7 int prevHold = hold, prevSold = sold, prevRest = rest; 8 hold = Math.max(prevHold, prevRest - prices[i]); 9 sold = prevHold + prices[i]; 10 rest = Math.max(prevRest, prevSold); 11 } 12 return Math.max(sold, rest); 13 } 14}

Related Problems