Problem · Dynamic Programming

Count Good Numbers

Learn this problem
MediumGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

Problem 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) → int

Examples

Example 1

m = 21return = 18

The 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.

More Google problems

drafts saved locally
public int countGoodNumbers(int m) {
  // Write your code here.
}
m21
expected18
checking account