Best Time to Buy and Sell Stock with Cooldown
Signals to notice
Brute force first
Try all buy/sell/cooldown sequences. Three choices at each day. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
The key insight
State DP: track three states per day — hold (own stock), sold (just sold), cooldown (resting). Transitions: hold = max(prevHold, prevCooldown - price), sold = prevHold + price, cooldown = max(prevCooldown, prevSold). Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
What must stay true
Three states capture all possibilities. The cooldown state enforces the one-day waiting period after selling. Each state depends only on the previous day's states. As long as that statement keeps holding, you can trust the steps built on top of it.
Easy way to go wrong
Forgetting that you can't buy the day after selling — that's what the cooldown state enforces. Without it, you'd accidentally allow immediate re-buying. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.