Dynamic Programming•Pattern: State Machine DP•Medium

Best time to buy and sell stock with cooldown

Problem Intuition & Objective

Maximize profit with a 1-day cooldown after selling.

Time ComplexityO(n)
Optimal
Space ComplexityO(1)
Memory Bounds
Optimal Algorithmic Approach

State-machine DP with three states.

Core Invariants & Edge Cases

  • States: held (holding stock), sold (just sold), rest (cooldown over)
  • Transitions: held = max(held, rest - price); sold = held + price; rest = max(rest, sold)
  • Answer is max(sold, rest) after all prices
Previous ProblemClimbing stairs with variable stepsNext Problem Matrix chain multiplication (minimum cost)

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.