Digit DP: Counting Numbers Up to N

Data Structures & Algorithms · Advanced Dynamic Programming · Master · 30 min

Count the numbers up to N (or in a range) that have a digit property by building them digit by digit, tracking whether you are still "tight" against N, and memoising (position, state, tight).

This lesson is part of TuringPath Pro. See plans.

What you will learn

  • Count by digits, not by numbers
  • The tight flag: are we still hugging N?
  • Memoise (pos, state, tight)
  • Ranges and leading zeros

More in Advanced Dynamic Programming

Previous: Tree DP: Answers That Flow Up · Next: Z-Function & Manacher's Algorithm