Practice Discrete Math

Algorithms / Dp State Design

Least You Need to Know: Dynamic Programming State Design

Dynamic programming starts by choosing a state that remembers exactly the information a smaller subproblem needs. Good state design is usually the difference between a clean recurrence and a confused one.

The least you need to know

Key notation

dp[i] answer for subproblem ending at or starting from index i
dp[r][c] answer for the subproblem at grid cell (r,c)
state minimal information needed to describe a subproblem

Tiny worked example

  • In the climb-stairs problem, a natural state is `dp[i] = number of ways to reach step i`.
  • The next move comes from step `i-1` or `i-2`.
  • That gives the recurrence `dp[i] = dp[i-1] + dp[i-2]`.
  • The good state uses just the step index because no other history matters.

Common mistakes

How to recognize this kind of problem

Start practice