#3519

Count Numbers with Non-Decreasing Digits

Hard
MathStringDynamic ProgrammingDigit DPCombinatorial Counting
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

Use digit dynamic programming to count valid numbers directly without generating them. This approach leverages the properties of non-decreasing sequences.

⚙️

Algorithm

3 steps
  1. 1Step 1: Define a DP function that counts non-decreasing numbers up to a given limit in base b.
  2. 2Step 2: Use the DP function to calculate counts for r and l-1, then subtract to get the count in the range [l, r].
  3. 3Step 3: Handle the base conversion and non-decreasing condition within the DP logic.
solution.py15 lines
1def countNonDecreasing(l, r, b):
2    def dp(pos, last_digit, tight, digits):
3        if pos == len(digits): return 1
4        if (pos, last_digit, tight) in memo: return memo[(pos, last_digit, tight)]
5        limit = digits[pos] if tight else b - 1
6        count = 0
7        for digit in range(last_digit, limit + 1):
8            count += dp(pos + 1, digit, tight and (digit == limit), digits)
9        memo[(pos, last_digit, tight)] = count
10        return count
11    def count_up_to(x):
12        digits = list(map(int, str(int(x, b))))
13        return dp(0, 0, True, digits)
14    memo = {}
15    return (count_up_to(r) - count_up_to(l) + 10**9 + 7) % (10**9 + 7)

Complexity note: The DP approach efficiently counts valid numbers without generating them, leading to linear complexity relative to the number of digits.

  • 1Non-decreasing sequences can be counted using combinatorial methods.
  • 2Dynamic programming can optimize counting by avoiding redundant calculations.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.