Count Numbers with Non-Decreasing Digits Problem
Description
LeetCode Problem 3519.
You are given two integers, l and r, represented as strings, and an integer b. Return the count of integers in the inclusive range [l, r] whose digits are in non-decreasing order when represented in base b.
An integer is considered to have non-decreasing digits if, when read from left to right (from the most significant digit to the least significant digit), each digit is greater than or equal to the previous one.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: l = "23", r = "28", b = 8
Output: 3
Explanation:
- The numbers from 23 to 28 in base 8 are: 27, 30, 31, 32, 33, and 34.
- Out of these, 27, 33, and 34 have non-decreasing digits. Hence, the output is 3.
Example 2:
Input: l = "2", r = "7", b = 2
Output: 2
Explanation:
- The numbers from 2 to 7 in base 2 are: 10, 11, 100, 101, 110, and 111.
- Out of these, 11 and 111 have non-decreasing digits. Hence, the output is 2.
Constraints:
1 <= l.length <= r.length <= 1002 <= b <= 10landrconsist only of digits.- The value represented by
lis less than or equal to the value represented byr. landrdo not contain leading zeros.
Solution
Approach: Digit DP in Base b
The numbers have up to 100 decimal digits, so we cannot walk through the range. Instead, count f(x), the number of integers in [1, x] whose base-b digits never decrease, and return f(r) - f(l) + (1 if l itself qualifies), modulo 109 + 7.
First convert l and r from decimal strings to base b by repeated long division. Precompute ways[len][d], the number of non-decreasing digit strings of length len that only use digits d to b - 1; it satisfies ways[len][d] = ways[len][d + 1] + ways[len - 1][d].
To compute f(x) with m base-b digits:
- Numbers with fewer digits: add
ways[len][1]for everylen < m(the first digit cannot be 0). - Numbers with
mdigits: walk the digits ofxfrom the left, tracking the previous digitprev. At positioniwith digitd, every smaller digitefrommax(prev, 1 if i == 0 else 0)up tod - 1can be placed there, and the rest is free: addways[m - i - 1][e]. Then stop ifd < prev(the prefix ofxitself is no longer valid); otherwise setprev = d. - If the whole of
xstayed non-decreasing, add 1 forxitself.
Complexity: O(L2 + L * b) time and O(L * b) space, where L is the number of digits in base b (at most about 333).
class Solution:
def countNumbers(self, l: str, r: str, b: int) -> int:
mod = 10**9 + 7
# ways[len][d]: non-decreasing digit strings of length len using digits d..b-1
size = len(r) * 4 + 5
ways = [[1] * (b + 1) for _ in range(size)]
for ln in range(1, size):
ways[ln][b] = 0
for d in range(b - 1, -1, -1):
ways[ln][d] = (ways[ln][d + 1] + ways[ln - 1][d]) % mod
def to_base(s: str) -> List[int]:
cur = [int(c) for c in s]
res = []
while cur:
rem = 0
nxt = []
for d in cur:
rem = rem * 10 + d
q = rem // b
rem %= b
if nxt or q:
nxt.append(q)
res.append(rem)
cur = nxt
return res[::-1]
def good(digits: List[int]) -> bool:
return all(digits[i] <= digits[i + 1] for i in range(len(digits) - 1))
def count(digits: List[int]) -> int:
# numbers in [1, x] with non-decreasing digits
m = len(digits)
res = 0
for ln in range(1, m):
res += ways[ln][1]
prev = 0
ok = True
for i, d in enumerate(digits):
lo = max(prev, 1 if i == 0 else 0)
for e in range(lo, d):
res += ways[m - i - 1][e]
if d < prev:
ok = False
break
prev = d
if ok:
res += 1
return res % mod
dl, dr = to_base(l), to_base(r)
return (count(dr) - count(dl) + (1 if good(dl) else 0)) % mod