Best Time to Buy and Sell Stock with Cooldown
Problem
You are given daily stock prices. You may complete as many transactions as you like (buy one, then sell it), but you cannot hold more than one share, and after you sell you must wait one day (cooldown) before buying again.
Return the maximum profit.
Example 1
- Input
prices = [1, 2, 3, 0, 2]- Output
3- Why
- Buy at 1, sell at 2, cool down, buy at 0, sell at 2.
Example 2
- Input
prices = [1]- Output
0
Constraints
- 1 ≤ prices.length ≤ 5000
- 0 ≤ prices[i] ≤ 1000
Explanation
At the end of each day you are in one of three states: holding a share, just sold (so tomorrow is a cooldown), or resting (free to buy).
Transitions: holding = max(keep holding, buy today from resting); sold = holding yesterday + today's price; resting = max(rest again, cooldown after yesterday's sale).
Approach 1 · Naive
Recursion over days and state
- Time O(2ⁿ)
- Space O(n)
On each day, decide based on whether you hold a share: buy or wait, or sell (skipping the next day) or wait.
best(day, holding): 0 past the last day.- Not holding:
max(best(day + 1, false), best(day + 1, true) - price). - Holding:
max(best(day + 1, true), best(day + 2, false) + price).
class Solution {
public int maxProfit(int[] prices) {
return best(prices, 0, false);
}
private int best(int[] prices, int day, boolean holding) {
if (day >= prices.length) {
return 0;
}
int wait = best(prices, day + 1, holding);
int act = holding
? best(prices, day + 2, false) + prices[day]
: best(prices, day + 1, true) - prices[day];
return Math.max(wait, act);
}
}class Solution:
def maxProfit(self, prices: List[int]) -> int:
def best(day, holding):
if day >= len(prices):
return 0
wait = best(day + 1, holding)
if holding:
act = best(day + 2, False) + prices[day]
else:
act = best(day + 1, True) - prices[day]
return max(wait, act)
return best(0, False)Approach 2 · Optimal
Three-state DP
- Time O(n)
- Space O(1)
Track the best profit for each state at the end of every day, using only yesterday's values.
hold = -prices[0],sold = 0,rest = 0.- Each day:
newHold = max(hold, rest - price),newSold = hold + price,newRest = max(rest, sold). - Return
max(sold, rest).
class Solution {
public int maxProfit(int[] prices) {
int hold = -prices[0];
int sold = 0;
int rest = 0;
for (int i = 1; i < prices.length; i++) {
int newHold = Math.max(hold, rest - prices[i]);
int newSold = hold + prices[i];
int newRest = Math.max(rest, sold);
hold = newHold;
sold = newSold;
rest = newRest;
}
return Math.max(sold, rest);
}
}class Solution:
def maxProfit(self, prices: List[int]) -> int:
hold, sold, rest = -prices[0], 0, 0
for price in prices[1:]:
hold, sold, rest = max(hold, rest - price), hold + price, max(rest, sold)
return max(sold, rest)Complexity compared
| Approach | Time | Space |
|---|---|---|
| Naive Recursion over days and state | O(2ⁿ) | O(n) |
| Optimal Three-state DP | O(n) | O(1) |
Key takeaways
- State machines tame every stock variant: add a state for the cooldown, a fee term, or a transaction counter.
Your notes
Saved in this browser as you type.
Solved it on your own?
Mark it done to track your progress, or star it to come back before an interview.