Track multiple dp states (e.g., holding/not-holding stock); transition between states each step
When to useStock buy/sell variants with cooldown, transaction limits, paint house
dp
Best Time To Buy And Sell Stockeasy
best_time_to_buy_and_sell_stockBest Time To Buy And Sell Stock IImedium
best_time_to_buy_and_sell_stock_iiDECLARE
one variable per state (e.g., hold, cash) or dp[i][state]
—
—
BASE CASES
init each state's value at step 0 (e.g., hold = -prices[0], cash = 0)
—
—
BUILD
for each step from 1 onward
—
—
[RECURRENCE]
compute next state values from current; update
if (price - minSoFar > best) best = price - minSoFar;
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
RETURN
return the desired final state
return best;
return profit;