Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

prices =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

State machine DP with cooldown.

Approach

Maintain three states:

  • hold: max profit holding a stock after day i
  • sold: 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 + p
  • hold = max(hold, rest - p)
  • rest = max(rest, prevSold) Answer = max(sold, rest).

Why it works

These states capture all valid actions while enforcing the cooldown after selling.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)
Solution
Updated: 2026-02-23

Idea

State machine DP with cooldown.

Approach

Maintain three states:

  • hold: max profit holding a stock after day i
  • sold: 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 + p
  • hold = max(hold, rest - p)
  • rest = max(rest, prevSold) Answer = max(sold, rest).

Why it works

These states capture all valid actions while enforcing the cooldown after selling.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)