Best Time to Buy and Sell Stock with Cooldown - Problem

Imagine you're a stock trader with access to future price information, but there's a catch - every time you sell stock, you must take a mandatory one-day vacation (cooldown period) before you can buy again!

Given an array prices where prices[i] represents the stock price on day i, your goal is to maximize your profit by strategically buying and selling shares.

Trading Rules:

  • πŸ”„ You can complete unlimited transactions (buy-sell pairs)
  • ⏸️ After selling, you must wait one day before buying again
  • 🚫 You cannot hold multiple shares simultaneously
  • πŸ’° Each transaction consists of buying one share and later selling it

Example: If prices = [1,2,3,0,2], you could buy on day 0 (price=1), sell on day 2 (price=3), cooldown on day 3, buy on day 3 (price=0), and sell on day 4 (price=2) for a total profit of (3-1) + (2-0) = 4.

Input & Output

example_1.py β€” Basic Trading
$ Input: [1,2,3,0,2]
β€Ί Output: 3
πŸ’‘ Note: Buy on day 0 (price=1), sell on day 2 (price=3), profit = 3-1 = 2. Then buy on day 3 (price=0), sell on day 4 (price=2), profit = 2-0 = 2. Total profit = 2 + 2 = 4. Wait, that's wrong - we need cooldown! Correct: Buy day 0 (price=1), sell day 2 (price=3), cooldown day 3, buy day 4 would be invalid. Optimal: buy day 0, sell day 1 (profit=1), cooldown day 2, buy day 3, sell day 4 (profit=2). Total = 3.
example_2.py β€” No Profit Scenario
$ Input: [1]
β€Ί Output: 0
πŸ’‘ Note: Only one day, so no transactions possible. Maximum profit is 0.
example_3.py β€” Declining Prices
$ Input: [5,4,3,2,1]
β€Ί Output: 0
πŸ’‘ Note: Prices are continuously declining, so buying any day and selling later would result in loss. Best strategy is to not trade at all, profit = 0.

Visualization

Tap to expand
Dynamic Programming State TransitionsExample: prices = [1, 2, 3, 0, 2]Day: 0 1 2 3 4HELDOwn StockDay 0: -1Day 1: -1SOLDJust SoldDay 1: 1Day 2: 2RESTCan BuyDay 2: 1Day 3: 2sell +pricecooldownbuy -priceFinal Calculationβ€’ held[i] = max(held[i-1], rest[i-1] - prices[i])β€’ sold[i] = held[i-1] + prices[i]β€’ rest[i] = max(rest[i-1], sold[i-1])Result: max(sold[4], rest[4]) = max(2, 3) = 3
Understanding the Visualization
1
Start in REST state
Day 0: We can buy stock, transitioning to HELD state
2
HELD state decisions
Each day: Hold stock or sell (go to SOLD state)
3
SOLD state cooldown
Must go to REST state next day (mandatory cooldown)
4
REST state options
Can stay in REST or buy stock (go to HELD)
5
Track maximum profit
For each state, keep the maximum profit achievable
Key Takeaway
🎯 Key Insight: Model the problem as a state machine with three states (HELD, SOLD, REST) and track the maximum profit achievable in each state at every day.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the prices array

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only storing three state variables

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ prices.length ≀ 5000
  • 0 ≀ prices[i] ≀ 1000
  • Cooldown constraint: After selling, must wait one day before buying
  • No simultaneous transactions: Must sell before buying again
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
73.0K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen