Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

Boss fightThe Cooldown Market

Learn
Play
Prove
Problem context and objectives
Mission briefing

Energy-route planner · A runner crosses a line of power stations

Build each decision from smaller outcomes already solved: The Cooldown Market.

Re-solving the same future branches drains exponential energy.

How you win

  1. 1Recognize when Stock trading with cooldown matches the clues
  2. 2Keep this true after every move: hold, sold, and rest are the best profits for their exact end-of-day states
  3. 3Reach the result within O(n) time and O(1) space

Rules and pressure

  • Target cost: O(n) time and O(1) space
  • State rule: hold, sold, and rest are the best profits for their exact end-of-day states
Lesson 1 of 3

Live algorithm trace

Stock trading with cooldown

Complete execution
1 of 5

After day zero, either hold the bought share or rest.

1hold = -first price; sold = -infinity; rest = 0
2for each later price
3newHold = max(hold, oldRest - price)
4newSold = hold + price
5newRest = max(rest, sold)
6return max(sold, rest)
hold = -1sold = -infrest = 0
Truth to preserve / Cost target
Truth to preserve

hold, sold, and rest are the best profits for their exact end-of-day states

Cost target

O(n) time and O(1) space

A day ends holding stock, newly sold, or resting without stock. Buying may only follow an older rest state, which enforces the one-day cooldown after a sale.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead