State machine DP with cooldown.
Maintain three states:
hold: max profit holding a stock after day isold: max profit just sold on day i (cooldown next day)rest: max profit not holding and not in cooldown
Transitions per price p:sold = hold + phold = max(hold, rest - p)rest = max(rest, prevSold)
Answer = max(sold, rest).These states capture all valid actions while enforcing the cooldown after selling.
State machine DP with cooldown.
Maintain three states:
hold: max profit holding a stock after day isold: max profit just sold on day i (cooldown next day)rest: max profit not holding and not in cooldown
Transitions per price p:sold = hold + phold = max(hold, rest - p)rest = max(rest, prevSold)
Answer = max(sold, rest).These states capture all valid actions while enforcing the cooldown after selling.