Watch 10 video solutions for Best Time to Buy and Sell Stock with Cooldown, a medium level problem involving Array, Dynamic Programming. This walkthrough by NeetCode has 146,609 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
Example 2:
Input: prices = [1] Output: 0
Constraints:
1 <= prices.length <= 50000 <= prices[i] <= 1000Problem Overview: You are given an array prices where prices[i] is the stock price on day i. You can buy and sell multiple times, but after selling a stock you must wait one day before buying again (cooldown). The goal is to compute the maximum profit achievable under this constraint.
Approach 1: Recursive DP with Memoization (Time: O(n), Space: O(n))
Model the decision process day by day. At each index you track whether you are allowed to buy or you currently hold a stock. From a buy state, you either buy the stock and move to the sell state or skip the day. From a sell state, you either sell the stock and jump two days forward (due to the cooldown) or skip the day. A naive recursion explores many repeated states, so memoization stores results for each pair (day, holdingState). This converts the exponential search into linear time because each state is solved once. This approach clearly expresses the decision tree and is useful when first reasoning about the dynamic programming state transitions.
Approach 2: Iterative Dynamic Programming (State Machine) (Time: O(n), Space: O(1))
The optimal solution tracks three states while scanning the array once. hold represents the best profit while holding a stock, sold represents profit immediately after selling, and rest represents profit while in cooldown or doing nothing. For each price you update the states using transitions: buying moves from rest → hold, selling moves from hold → sold, and waiting moves from sold → rest. Because each state depends only on the previous day, you keep three variables instead of an entire DP table. The algorithm performs a single pass over the prices array, updating profits in constant time per element.
The key insight is separating "holding", "just sold", and "resting" states. The cooldown restriction is naturally enforced because buying is only allowed from the rest state, not from sold. This transforms the problem into a small deterministic state machine, a common pattern in dynamic programming problems involving stock trading constraints.
Recommended for interviews: The iterative dynamic programming state-machine approach. Interviewers expect an O(n) solution with constant space and clear state transitions. Starting with the recursive memoized version demonstrates understanding of the decision process, but converting it into the compact three-state DP shows strong optimization and problem modeling skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive DP with Memoization | O(n) | O(n) | When reasoning about decisions (buy, sell, skip) and building intuition for the DP states |
| Iterative Dynamic Programming (State Machine) | O(n) | O(1) | Optimal solution for interviews and production due to constant space and single pass |