Problem context and objectives
Mission briefing
Energy-route planner · A runner crosses a line of power stations
Build each decision from smaller outcomes already solved: The Cooldown Market.
Re-solving the same future branches drains exponential energy.
How you win
- 1Recognize when Stock trading with cooldown matches the clues
- 2Keep this true after every move: hold, sold, and rest are the best profits for their exact end-of-day states
- 3Reach the result within O(n) time and O(1) space
Rules and pressure
- Target cost: O(n) time and O(1) space
- State rule: hold, sold, and rest are the best profits for their exact end-of-day states
Lesson 1 of 3
Live algorithm trace
Stock trading with cooldown
Complete execution1 of 5
After day zero, either hold the bought share or rest.
1
hold = -first price; sold = -infinity; rest = 02
for each later price3
newHold = max(hold, oldRest - price)4
newSold = hold + price5
newRest = max(rest, sold)6
return max(sold, rest)hold = -1sold = -infrest = 0
Truth to preserve / Cost target
Truth to preserve
hold, sold, and rest are the best profits for their exact end-of-day states
Cost target
O(n) time and O(1) space
A day ends holding stock, newly sold, or resting without stock. Buying may only follow an older rest state, which enforces the one-day cooldown after a sale.
Your call · What should guide every step of this algorithm?