Glow Market Profit Planner
This problem is about buying and selling over time to maximize profit, but there is a cooldown rule after each sale. That means you cannot instantly buy again on the very next day.
Because of that cooldown, the best plan is not always just taking every local profit. Selling today might force you to sit out tomorrow, so each decision affects the next few days instead of only the current one.
For example, if prices = [1,2,3,0,2], the answer is 3. One optimal strategy is to buy at 1, sell at 3, wait through the cooldown, then buy at 0 and sell at 2. If prices = [], the answer is 0 because there are no trades to make.
So the task is to maximize total trading profit while respecting the cooldown after every sale.
Example Input & Output
Buy on day 1, sell on day 3, cooldown day 4, buy day 4, sell day 5.
No trading nights yield zero profit.
Buy on day 2 and sell on day 3 after waiting through the first decline.
Algorithm Flow

Solution Approach
This is a classic stock DP with a few states that describe what situation you are in on each day.
A common setup is:
hold: best profit if you are currently holding a stock
sold: best profit if you just sold today
rest: best profit if you are not holding and not in the middle of a fresh sale today
Then each day updates these states:
The cooldown effect is handled by the fact that you can only buy from the rest state, not directly from a same-day sold state.
After processing all prices, the answer is the best profit in a non-holding state, usually Math.max(rest, sold). This runs in linear time and only needs constant extra space.
Best Answers
class Solution {
public int calculate_max_profit(int[] prices) {
if (prices.length == 0) return 0;
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int p : prices) {
if (p < minPrice) minPrice = p;
else if (p - minPrice > maxProfit) maxProfit = p - minPrice;
}
return maxProfit;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
