← DiffPush

Best Time to Buy and Sell Stock with Cooldown

Standard Bar Dynamic Programming · DP on Stocks O(n) · O(n)

The Cooling-Down Cargo Bay

After every release, the broker's cargo bay must pass a one-hour safety inspection before another lock can open — a mandatory cooldown. Trades remain unlimited otherwise. Dispatch needs the schedule that still maximizes total spread while respecting the forced stand-down hour after each sale.

Input: An array prices where prices[i] is the fee on hour i.

Output: Return the maximum total profit with unlimited transactions and a one-hour cooldown after every sell.

Constraints

Examples

Example 1

Input: {"prices":[1,2,3,0,2]}
Output: 3
Lock at 1, release at 2, stand down at 0, lock at 0, release at 2 — total 3.

Example 2

Input: {"prices":[1,2,4]}
Output: 3
A single lock at 1 and release at 4 clears the cooldown for free.

Solve this in your browser →

Also on LeetCode ↗