309. Best Time to Buy and Sell Stock with Cooldown
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- 2-D Dynamic Programming
- Time
- O(n)
- Space
- O(1)
Standard Solution (C++)
SummaryGiven a sequence of daily stock prices, find the maximum profit achievable by buying and selling shares, where selling a stock requires a one-day cooldown before buying again (and you can hold at most one share at a time).
Approach
- Model each day as one of three states: holding a stock, cooling down (just sold), or idle (free to buy).
- Transitions:
hold = max(hold, idle - price)(keep holding or buy from idle);cooldown = hold + price(sell today);idle = max(idle, cooldown)(stay idle or come off cooldown). - Because each state depends only on the previous day's values, we only need O(1) extra space — no full DP table required.
- The answer is
max(idle, cooldown)on the last day (we never end in a better state by still holding).
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxProfit(vector<int>& prices) {
// Three states at each day:
// hold – currently holding a share
// cooldown – just sold (must wait one day before buying)
// idle – not holding, not in cooldown (free to buy)
//
// Use INT_MIN/2 as "impossible" sentinel to avoid signed-integer overflow
// when computing cooldown = prevHold + price for the initial impossible state.
int hold = INT_MIN / 2; // impossible until we buy
int cooldown = 0;
int idle = 0;
for (int price : prices) {
int prevHold = hold;
int prevCooldown = cooldown;
int prevIdle = idle;
// Buy from idle state, or continue holding
hold = max(prevHold, prevIdle - price);
// Sell today (must have been holding)
cooldown = prevHold + price;
// Stay idle, or transition out of cooldown
idle = max(prevIdle, prevCooldown);
}
// Best profit when not holding at the end
return max(idle, cooldown);
}
};
Time O(n) Space O(1)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
/*
int maxProfit(vector<int>& prices) {
int numOfDays=prices.size();
if(numOfDays<2)
return 0;
vector<vector<int>> dp(numOfDays, vector<int>(2, 0)); // max profit (end with hold, end with notHold)
// initialization
dp[0][0]=-prices[0];
dp[0][1]=0;
dp[1][0]=max(-prices[0], -prices[1]);
dp[1][1]=max(0, dp[0][0]+prices[1]);
for(int i=2; i<numOfDays; i++){
dp[i][0]=max(dp[i-1][0], dp[i-2][1]-prices[i]);
dp[i][1]=max(dp[i-1][1], dp[i-1][0]+prices[i]);
}
return dp[numOfDays-1][1];
}
*/
// dp with O(1) space
int maxProfit(vector<int>& prices) {
int numOfDays=prices.size();
if(numOfDays<2)
return 0;
int prevHold, hold, prevNotHold, notHold;
prevHold=-prices[0];
hold=max(prevHold, -prices[1]);
prevNotHold=0;
notHold=max(prevNotHold, prevHold+prices[1]);
for(int i=2; i<numOfDays; i++){
prevHold=hold;
hold=max(hold, prevNotHold-prices[i]);
prevNotHold=notHold;
notHold=max(notHold, prevHold+prices[i]);
}
return notHold;
}
};
// Time Complexity: O(n)
// Space Complexity: O(1) Time O(n) Space O(1)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.