Best Time to Buy and Sell Stock with Cooldown
You are given an array prices where prices[i] is the price of a given stock on the ith day. Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to cooldown one day after selling.
Examples
prices = [1,2,3,0,2]
3
Transactions: buy at 1, sell at 2, cooldown, buy at 0, sell at 2. Profit = 1 + 2 = 3.
prices = [1]
0
No transactions can be done.
Constraints
- •
1 <= prices.length <= 5000 - •
0 <= prices[i] <= 1000
Approaches
Recursively try all states: buy, sell, or cooldown.
def max_profit(prices):
def helper(i, holding):
if i >= len(prices):
return 0
if holding:
return max(prices[i] + helper(i + 2, False), helper(i + 1, True))
else:
return max(-prices[i] + helper(i + 1, True), helper(i + 1, False))
return helper(0, False)Use three states: hold, sold, rest.
def max_profit(prices):
if not prices:
return 0
n = len(prices)
hold = [0] * n
sold = [0] * n
rest = [0] * n
hold[0] = -prices[0]
for i in range(1, n):
hold[i] = max(hold[i-1], rest[i-1] - prices[i])
sold[i] = hold[i-1] + prices[i]
rest[i] = max(rest[i-1], sold[i-1])
return max(sold[-1], rest[-1])Optimize space by only keeping previous values.
Diagram
def max_profit(prices):
if not prices:
return 0
hold = -prices[0]
sold = 0
rest = 0
for i in range(1, len(prices)):
prev_hold = hold
prev_sold = sold
hold = max(prev_hold, rest - prices[i])
sold = prev_hold + prices[i]
rest = max(rest, prev_sold)
return max(sold, rest)Complexity Comparison
| Approach | Time | Space | Description |
|---|---|---|---|
| Recursion | O(2^n) | O(n) | Recursively try all states: buy, sell, or cooldown. |
| DP - State Machine | O(n) | O(n) | Use three states: hold, sold, rest. |
| Space Optimized State Machine | O(n) | O(1) | Optimize space by only keeping previous values. |
Recursively try all states: buy, sell, or cooldown.
Use three states: hold, sold, rest.
Optimize space by only keeping previous values.
Common Mistakes
Not handling the cooldown correctly (must wait one day after selling)
Using a 2D DP when state machine is cleaner
Forgetting that you can hold at most one share