Dynamic Programming21 sections · 916 units
Open in Course

Vocabulary - Leading Zeros

Handling shorter numbers

When counting up to N=325N = 325, you also count 11, 4242, 9999, numbers with fewer digits. How do you handle this in digit DP (dynamic programming)? Track a "started" flag. Before placing the first non-zero digit, you haven't started yet. A leading zero means you're still choosing whether to use this position.

If started is false and you place 00, you're skipping this position (like choosing a 2-digit number instead of 3-digit). If you place non-zero, started becomes true and stays true.