“Know how to solve every problem that has been solved.” “What I cannot create, I do not understand.” — Richard Feynman

Designing the state: the part nobody teaches

Learn

Lesson 6 of 13 standard ~8 min

Most DP tutorials hand you the recurrence and let you marvel at it. In a contest nobody hands you anything — and the hard 80% of DP is not the transition, it is choosing the state: the minimal set of facts about "what I have decided so far" that makes every remaining decision correct without remembering anything else. Get the state right and the transition is usually obvious. Get it wrong and no cleverness saves you.

The test to run in your head: what is the least I need to know about the prefix I have already processed to make all future choices correctly? Too little state and the recurrence is simply wrong — the future depends on something you forgot. Too much state and the table explodes into TLE or MLE. The art is landing exactly on "enough."

Look at three familiar problems through this lens. Coin change: the only thing that matters about your past choices is how much amount remains — state is (amount). 0/1 knapsack: which items you considered and how much capacity you have spent — state is (item index, capacity used). Longest increasing subsequence: where you are and the value you last picked — state is (index, last value).

Watch a state get designed. "Max sum of non-adjacent elements" (house robber): the future depends on your position and one bit — did I take the previous element? That gives dp[i][took_prev]. But notice the bit only forbids taking i when it is set, so you can collapse it to two running numbers — best-ending-here-taken and best-ending-here-skipped — and the whole DP is O(1) memory. The state told you that.

standardMultiple choice

For "maximum sum of a subsequence with no two chosen elements adjacent" (house robber) over an array, what is a sufficient and minimal DP state?

The recurring dimensions to scan for: a position (index, node, digit), a resource budget (capacity, time, k uses left), a last-choice constraint (last value, last color), a flag (parity, tight, took-previous), and in games whose turn it is. Most DP states are two or three of these stapled together — and the single most common bug is carrying a dimension you did not actually need.

standardMultiple choice

A contestant codes 0/1 knapsack with the state (item index, capacity used, current total value) and gets Memory Limit Exceeded. Which dimension is redundant and causing the blowup?

Everything in the rest of this unit — bitmask, tree, digit DP — is the same discipline with a more exotic state: a subset packed into an integer, a subtree, a partially-built number under a bound. Learn to ask "what is the minimal summary of the past?" and the named techniques become variations on one move.