Numbers At Most N Given Digit Set Problem
Description
LeetCode Problem 902.
Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'.
Return the number of positive integers that can be generated that are less than or equal to a given integer n.
Example 1:
Input: digits = ["1","3","5","7"], n = 100
Output: 20
Explanation:
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
Example 2:
Input: digits = ["1","4","9"], n = 1000000000
Output: 29523
Explanation:
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits array.
Example 3:
Input: digits = ["7"], n = 8
Output: 1
Constraints:
- 1 <= digits.length <= 9
- digits[i].length == 1
- digits[i] is a digit from '1' to '9'.
- All the values in digits are unique.
- digits is sorted in non-decreasing order.
- 1 <= n <= 10^9
Solution
Approach: Count Digit by Digit
Generating the numbers one by one is too slow (Example 2 already has 29523, and with all nine digits there are hundreds of millions). Count them instead. Let L be the number of digits of n and d the number of available digits. With d choices per position there are dk numbers of exactly k digits.
Shorter numbers. Every number with fewer than L digits is smaller than n, so add d1 + d2 + … + dL-1.
Numbers with L digits. A number of the same length is smaller than n when it matches n on some prefix and then has a smaller digit. Go through the positions of n from left to right, assuming the earlier positions match n:
- Count the available digits smaller than
n[i]. Each one can be followed by anything in the remainingL - i - 1positions, so addsmaller * d^(L - i - 1). - If
n[i]itself is not available, no number can keep matchingn, so stop and return the count. - If every digit of
nis available,nitself can be written: add 1.
Step 2 is the part that is easy to miss. Without it, the count would include numbers built on a prefix of n that cannot be written.
For digits = ["1","3","5","7"] and n = 100: the shorter numbers give 4 + 16 = 20. At the first position no digit is smaller than 1 and 1 is available. At the second, no digit is smaller than 0 and 0 is not available, so we stop at 20.
Complexity: O(L · d) time and O(L) space, where L is the number of digits of n (at most 10) and d is the length of digits.
class Solution:
def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
target = str(n)
length = len(target)
# powers[i] = how many i-digit numbers can be written with the given digits
powers = [1] * length
for i in range(1, length):
powers[i] = powers[i - 1] * len(digits)
# Every number with fewer digits than n is smaller than n.
count = 0
for size in range(1, length):
count += powers[size]
# Numbers with as many digits as n: they match n on a prefix, then have a smaller digit.
for i in range(length):
smaller = 0
for d in digits:
if d < target[i]:
smaller += 1
count += smaller * powers[length - i - 1] # the remaining positions are free
if target[i] not in digits:
return count # the prefix of n cannot be continued
return count + 1 # every digit of n is available, so n itself counts