Core pattern

State Machine DP

Step 8 of 14Pattern 6 of 7
×
Pattern 6

How to recognize it fast

This is the pattern for timeline problems where the situation you are in today changes what you are allowed to do tomorrow.

When you see

buy / sell stockcooldowntransaction feelimited transactionsactions over time

Think

  • What state am I in today?
  • Am I holding, resting, cooling down, or spending one of a limited number of actions?
Execution

What to do once the pattern is clear

Core trick

  • Track the situation, not just the profit.
  • Each day transitions between a small number of valid states.
  • Once the states are explicit, the recurrence is often very short.

Shortcut recognition

Yesterday changes todayHolding versus not holding mattersA finite set of action states over time
Hidden trick

Students often store only one running profit and lose the real constraint. State machine DP works because you model the mode you are currently in.

Typical stock statesholding, notHolding, cooldown, transactionsLeft
Practice set

Questions covered