Dynamic Programming21 sections · 916 units
Open in Course

Core Concept

Digit-by-digit processing

Digit DP (dynamic programming) processes numbers as sequences of digits. To count integers from 11 to NN, you represent NN as a string and build valid numbers position by position.

For example, if N=325N = 325, you process three positions. At each position, you decide which digit to place, subject to constraints. Some digits might violate your condition (like repeating a digit when you want all distinct).

The observation: instead of iterating over all numbers, you iterate over all ways to fill digit positions. This turns O(N)O(N) into O(digits×states)O(\text{digits} \times \text{states}), which is tiny even for N=1018N = 10^{18}.