Best Time to Buy and Sell Stock with Cooldown
Medium
Topics
Given daily stock prices, maximize profit with as many transactions as you like, but after you sell you must cooldown one day before buying again. Return the max profit.
Example 1
Bars are the daily prices. Only one share may be held at a time, and after every sale there is a one-day cooldown before buying again — so a profitable pair of days is not always worth taking, because it can block a better pair right after it. Best achievable profit: 3.
Bars are the daily prices, left to right.
Input: prices = [1,2,3,0,2] Output: 3 Explanation: buy, sell, cooldown, buy, sell.
Example 2
Input: prices = [1] Output: 0
Constraints
- 1 <= prices.length <= 5000
- 0 <= prices[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
Track three daily states: hold (currently own a share), sold (just sold today), and rest (idle, free to buy). The cooldown is enforced because you can only buy from rest, and selling moves you to sold which becomes rest tomorrow.
Approach
- hold = max(hold, rest - price) — keep holding or buy from rest.
- sold = hold + price — sell what you held.
- rest = max(rest, prev_sold) — stay idle or come off cooldown.
Dry Run
prices [1, 2, 3, 0, 2]. Three states, each the best profit achievable while in it:
hold = holding a share, sold = just sold today, rest = idle and free to buy.
day price hold sold rest
-- -- -inf 0 0
0 1 -1 -inf 0
1 2 -1 1 0
2 3 -1 2 1
3 0 1 -1 2
4 2 1 3 2
answer max(sold, rest) = 3 -- buy at 1, sell at 2, cooldown, buy at 0, sell at 2
`prev = sold` is captured before sold is overwritten, and that single line IS the
cooldown. rest must inherit YESTERDAY's sold, because selling forces an idle day;
feed it today's and you can rest on the same day you sold, which returns 4 instead
of 3 here.
hold starts at negative infinity rather than 0. Seeding it at 0 claims you can
already be holding a share having paid nothing, which inflates everything: on
[1,2,3,0,2], [1] and [1,2,3,4,5] it gives 4, 1 and 5 against the correct 3, 0
and 4.
Watch hold on day 3: it rises from -1 to 1, because rest has reached 2 and the
price has dropped to 0, so re-entering costs nothing and banks the earlier profit.
That is the whole algorithm in one cell.Complexity
- Time — O(n): one pass, constant work per day.
- Space — O(1): three rolling variables.
Common Pitfalls
- Save the previous
soldbefore overwriting it.restmust take yesterday'ssold, because the cooldown means a sale makes you idle the next day -- using the fresh value lets you rest on the same day you sold and returns 4 instead of 3 on the first example. holdstarts at negative infinity, not 0. Seeding it at 0 says you can be holding a share having paid nothing, which inflates every answer: 4, 1, 4, 11 against the correct 3, 0, 3, 6.
Full Solution — all languages
class Solution:
def maxProfit(self, nums):
hold=float('-inf'); sold=0; rest=0
for p in nums:
prev=sold; sold=hold+p; hold=max(hold,rest-p); rest=max(rest,prev)
return max(sold,rest)import java.util.*;
class Solution {
public int maxProfit(int[] nums) {
long hold=-1000000000L,sold=0,rest=0;
for(int p:nums) {
long prev=sold;
sold=hold+p;
hold=Math.max(hold,rest-p);
rest=Math.max(rest,prev);
}
return (int)Math.max(sold,rest);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int maxProfit(vector<int>& nums) {
long long hold=-1000000000LL,sold=0,rest=0;
for(int p:nums) {
long long prev=sold;
sold=hold+p;
hold=max(hold,rest-(long long)p);
rest=max(rest,prev);
}
return (int)max(sold,rest);
}
};/**
* @param {number[]} nums
* @return {number}
*/ var maxProfit = function(nums) {
let hold=-Infinity,sold=0,rest=0;
for(const p of nums) {
const prev=sold;
sold=hold+p;
hold=Math.max(hold,rest-p);
rest=Math.max(rest,prev);
}
return Math.max(sold,rest);
};