Best Time to Buy and Sell Stock with Cooldown
Best Time to Buy and Sell Stock with Cooldown is LeetCode 309 (Medium). You get an array prices, where prices[i] is the price of one share on day i. Return the maximum profit you can make.
- You may trade as often as you like, but you can own at most one share at a time, so you sell before you buy again.
- The day after any sale is a cooldown day: you cannot buy on it.
- Never trading is allowed, so the answer is at least 0.
There are at most 5000 days, so one pass that carries a few numbers per day is all the work needed.
- 1 <= prices.length <= 5000
- 0 <= prices[i] <= 1000
Intuition
Without the cooldown you could collect every price rise. With it, selling today blocks tomorrow's buy, so grabbing a small rise can cost a bigger one right after it. What you may do today depends on what you did yesterday.
So keep, for each day, the best profit of every situation you can end that day in:
- hold: you own a share.
- sold: you sold today, so tomorrow you may not buy.
- rest: you own nothing and are free to buy tomorrow.
The whole cooldown rule is one missing arrow: a buy can only start from rest, never from sold. Every sale has to pass through one rest day before the next buy.
A rule that depends on your previous action (a rest day, a lockout, a limit on back-to-back moves) means one running total is not enough. Name each situation you can be in at the end of a day, and write which situations can follow which.
Approach
Take [1, 3, 2, 5]. Collecting both rises gives 5, but one of those buys lands on a cooldown day. Find the real best by hand, then decide which three numbers you would carry from day to day.
Set up day 0
Start with hold = -prices[0] (you bought the first share), sold = 0 and rest = 0. Nobody can sell on day 0, but a sold of 0 is harmless: it only ever flows into rest, which is already 0.
Sold: sell the share you held yesterday
Save yesterday's sold, then set sold = hold + price. The only way to end a day just after selling is to have held a share the evening before, so there is nothing to compare against.
Hold: keep the share or buy one
hold = max(hold, rest - price). You either keep yesterday's share, or you buy today using yesterday's rest profit. Taking money from yesterday's sold instead would mean buying the day after a sale.
Rest: wait, or finish the cooldown
rest = max(rest, prev_sold). Either you were already resting, or yesterday you sold and today is the forced rest day. This line needs the sold value from before this day's update, which is why it was saved first.
Why three numbers are enough
Every possible trading plan ends each day in exactly one of the three situations, and what it may do next depends only on which one. Keeping the best profit for each situation therefore never throws away a plan that could still win.
Return the best state without a share
Answer max(sold, rest). Ending the last day in hold means you paid for a share you never sold, which is always worse than not having bought it.
Code in Python, C++ and Java
Common pitfalls
Summing every rise as if there were no cooldown
for i in range(1, len(prices)):
profit += max(0, prices[i] - prices[i - 1])hold = max(hold, rest - price)
On [1, 3, 2, 5] the sum of rises is 5: sell at 3 on day 1, buy at 2 on day 2. Day 2 is the cooldown day, so that buy is illegal. The real best is 4, one trade from 1 to 5.
Buying out of yesterday's sale
hold = max(hold, max(rest, prev_sold) - price)
hold = max(hold, rest - price)
Letting hold read sold allows a buy the very next day. On [1, 2, 1, 2] that returns 2 (two back-to-back trades), but after selling on day 1 you cannot buy on day 2, so the answer is 1.
Letting rest read today's sale
sold = hold + price hold = max(hold, rest - price) rest = max(rest, sold)
prev_sold = sold sold = hold + price hold = max(hold, rest - price) rest = max(rest, prev_sold)
With today's sold in it, rest counts the sale day as the rest day, and tomorrow's buy skips the cooldown. On [2, 5, 1, 4] this gives 6 instead of 3.
Edge cases
[5]The loop never runs; sold and rest are both 0, so the answer is 0.
[3, 2, 1]Every sale is below its buy price, so sold stays negative and rest stays 0. Doing nothing wins.
Complexity
Best Time to Buy and Sell Stock with Cooldown FAQ
How is this different from Best Time to Buy and Sell Stock II?
Problem 122 has no rest day, so every rise can be collected and two states (holding, not holding) are enough. Here a sale blocks the next day, so "not holding" splits into sold and rest, and only rest may buy.
Can I solve it with recursion and memoization instead?
Yes. Let best(i, holding) either skip day i or trade on it, and make a sale jump to day i + 2 so the cooldown day is skipped. It is also O(n), but with 5000 days Python's default recursion limit of 1000 is too low, so the loop is the safer choice.
Does the cooldown also block selling?
No. Only buying is blocked, and only on the single day right after a sale. You may hold a share through as many days as you like and sell on any of them.