Problem. prices[i] is a stock's price on day i. You may complete as many transactions as you like (buy one share, then sell
it), holding at most one share at a time, but after you sell, you can't buy on the next day (a one-day cooldown). Return
the maximum profit.
prices = [1, 2, 3, 0, 2] → 3 (buy 1, sell 2, cooldown, buy 0, sell 2)
prices = [1] → 0
The decision on each day depends on what you're doing right now: holding a share, having just sold (so today is a cooldown), or free to buy. Those three states, plus the day, are the whole DP: a state machine whose best value per state is updated once per day.
ML context. "Best total reward over time, where the allowed actions depend on the current state" is a Markov decision process. Finite-horizon value iteration in reinforcement learning computes, for every day and state, the best achievable return: exactly this lesson's table (Change D). The same state-machine DP schedules actions with recovery periods: retraining jobs with a cool-down between deployments, or rate-limited API calls.