Best Time to Buy and Sell Stock with Cooldown
Implement maxProfit
This is the unlimited-trades stock problem with one extra rule: right after selling, you must skip a full day before you're allowed to buy again — a mandatory cooldown.
Without the cooldown, tracking whether you're currently holding a share is enough state to make every decision. The cooldown breaks that, because "just sold" and "free to buy" are no longer the same situation — a share sold yesterday can't be replaced today no matter how attractive the price looks. Splitting "not holding" into two separate states — just sold (cooldown pending) and resting (free to act) — restores a clean one-pass update: each day's hold, sold, and resting totals depend only on yesterday's three totals, with the cooldown rule baked directly into which of yesterday's states feeds into today's hold.
Example 1:
Input: prices = [1,2,3,0,2]
Output: 3
Example 2:
Input: prices = [1]
Output: 0
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ prices.length ≤ 12 - ●
0 ≤ prices[i] ≤ 1000
prices =
[1, 2, 3, 0, 2]