VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximizing-stock-profit-with-cooldown/

⇱ Stock Buy and Sell with Cooldown - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stock Buy and Sell with Cooldown

Last Updated : 4 Nov, 2025

Given an array arr[], where the ith element of arr[] represents the price of a stock on the ith day (all prices are non-negative integers). Find the maximum profit you can make by buying and selling stocks such that after selling a stock, you cannot buy again on the next day (i.e., there is a one-day cooldown).

Examples:

Input: arr[] = [0, 2, 1, 2, 3]
Output: 3
Explanation: You first buy on day 1, sell on day 2 then cool down, then buy on day 4, and sell on day 5. The total profit earned is (2-0) + (3-2) = 3, which is the maximum achievable profit.

Input: arr[] = [3, 1, 6, 1, 2, 4]
Output: 7
Explanation: You first buy on day 2 and sell on day 3 then cool down, then again you buy on day 5 and then sell on day 6. Clearly, the total profit earned is (6-1) + (4-2) = 7, which is the maximum achievable profit.

[Naive Approach] Using Recursion - O(2n) Time and O(n) Space

The idea is to use recursion to explore all possible actions on each day β€” whether to buy, sell, or skip.

At any given day, we track whether we currently hold a stock using a variable buy:

  • buy == 0 β†’ we don’t own a stock (we can buy).
  • buy == 1 β†’ we already own a stock (we can sell).

For each day i, there are two possible actions:

  • If we can buy (buy == 0): We can either buy the stock (subtract its price from profit) or skip to the next day.
  • If we can sell (buy == 1): We can either sell the stock (add its price to profit and subtract the transaction fee k) or skip to the next day.

At each step, we take the maximum of these two choices. The recursion continues until we reach the end of the array, and the maximum profit obtained represents the final answer.


Output
3

[Better Approach 1] Using Top Down Dp (Memorization) - O(n) Time and O(n) Space

The idea is to optimize the recursive solution by avoiding repeated calculations using memorization (top-down DP).

In the recursive approach, the same subproblems defined by the current index i (day number) and the state buy (whether we can buy or need to sell) are solved multiple times.

To handle this efficiently, we store the results of these subproblems in a 2D DP array dp[i][buy], where:

  • i represents the current day (0 to n-1), and
  • buy represents the action state (1 β†’ can buy, 0 β†’ can sell).

Whenever a subproblem is revisited, we directly return the stored value instead of recalculating it.


Output
3

[Better Approach 2] Using Bottom Up Dp (Tabulation) - O(n) Time and O(n) Space

The idea is to use tabulation (bottom-up DP) approach and build the solution iteratively.

We create a 2D DP table dp[i][buy] where:

  • dp[i][1] represents the maximum profit starting from day i when we can buy.
  • dp[i][0] represents the maximum profit starting from day i when we can sell.

We fill this table from the last day toward the first because each state depends on future days (i+1 or i+2).

At each step:

  • If we can buy, we choose between buying (-arr[i] + dp[i+1][0]) or skipping (dp[i+1][1]).
  • If we can sell, we choose between selling (arr[i] + dp[i+2][1]) or skipping (dp[i+1][0]).

The final answer will be stored in dp[0][1], which represents the maximum profit starting from the first day when buying is allowed.


Output
3

[Expected Approach] Using Space Optimization - O(n) Time and O(1) Space

In the tabulation (bottom-up) approach, we used a 2D dp array of size (n + 2) Γ— 2 β€” where each cell dp[i][buy] stored the maximum profit from day i onward.

But if you observe carefully, to compute dp[i][buy], we only need the values from dp[i + 1] and dp[i + 2].

So, instead of storing the entire table, we can just keep track of:

  • ahead1 β†’ the results of the next day (dp[i + 1])
  • ahead2 β†’ the results of the day after next (dp[i + 2])
  • curr β†’ the current day results (dp[i])

This way, we iteratively calculate results from the end (last day) toward the beginning while reusing only these three arrays.


Output
3
Comment