Problem · Dynamic Programming
Count Good Numbers
Learn this problemProblem statement
A positive integer is good when it satisfies all of the following conditions:
- It does not contain the digit
0. - Each digit appears at most once.
- No interior digit is smaller than both of its adjacent digits.
Given a positive integer m, return the number of good integers in the inclusive range [1, m].
Function
countGoodNumbers(m: int) → intExamples
Example 1
m = 21return = 18The good numbers are 1 through 9, 12 through 19, and 21. The numbers 10 and 20 contain 0, while 11 repeats a digit. Two-digit numbers have no interior digit.