Dynamic Programming21 sections · 916 units
Open in Course

Digit Set - State Definition

What dp[pos][tight] means

Define dp(pos,tight)dp(pos, tight) = count of valid numbers using positions pospos to end, given whether we're tight. No "started" flag needed here because all digits in the set are non-zero (the problem guarantees digits are from 11-99).

Every number we form is positive. The answer is the sum of: (1) all numbers shorter than nn, plus (2) dp(0,true)dp(0, true) for numbers with the same length as nn. This state captures all the information needed to count valid numbers from position pos onward.