Problem solution · Python

Count Numbers with Non Decreasing Digits

Count Numbers with Non Decreasing Digits: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Direct simulation
Source
Kamyu LeetCode Solutions
Length
109 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Count Numbers with Non Decreasing Digits, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 109 lines of Python from the credited upstream file count-numbers-with-non-decreasing-digits.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeCount Numbers with Non Decreasing Digits · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(n^2), n = len(r)# Space: O(n) # math, stars and bars, combinatoricsclass Solution(object):    def countNumbers(self, l, r, b):        """        :type l: str        :type r: str        :type b: int        :rtype: int        """        MOD = 10**9+7        fact, inv, inv_fact = [[1]*2 for _ in xrange(3)]        def nCr(n, k):            while len(inv) <= n:  # lazy initialization                fact.append(fact[-1]*len(inv) % MOD)                inv.append(inv[MOD%len(inv)]*(MOD-MOD//len(inv)) % MOD)  # https://cp-algorithms.com/algebra/module-inverse.html                inv_fact.append(inv_fact[-1]*inv[-1] % MOD)            return (fact[n]*inv_fact[n-k] % MOD) * inv_fact[k] % MOD         def nHr(n, k):            return nCr(n+k-1, k)         def count(x):            digits_base = []            while x:                x, r = divmod(x, b)                digits_base.append(r)            digits_base.reverse()            if not digits_base:                digits_base.append(0)            result = 0            for i in xrange(len(digits_base)):                if i-1 >= 0 and digits_base[i-1] > digits_base[i]:                    break                for j in xrange(digits_base[i-1] if i-1 >= 0 else 0, digits_base[i]):                    result = (result + nHr((b-1)-j+1, len(digits_base)-(i+1))) % MOD            else:                result = (result+1)%MOD            return result         return (count(int(r)) - count(int(l)-1)) % MOD  # Time:  O(n^2), n = len(r)# Space: O(n)# math, stars and bars, combinatoricsclass Solution2(object):    def countNumbers(self, l, r, b):        """        :type l: str        :type r: str        :type b: int        :rtype: int        """        MOD = 10**9+7        fact, inv, inv_fact = [[1]*2 for _ in xrange(3)]        def nCr(n, k):            while len(inv) <= n:  # lazy initialization                fact.append(fact[-1]*len(inv) % MOD)                inv.append(inv[MOD%len(inv)]*(MOD-MOD//len(inv)) % MOD)  # https://cp-algorithms.com/algebra/module-inverse.html                inv_fact.append(inv_fact[-1]*inv[-1] % MOD)            return (fact[n]*inv_fact[n-k] % MOD) * inv_fact[k] % MOD         def nHr(n, k):            return nCr(n+k-1, k)         def decrease(digits):            for i in reversed(xrange(len(digits))):                if digits[i]:                    digits[i] -= 1                    break                digits[i] = 9         def divide(digits, base):            result = []            r = 0            for d in digits:                q, r = divmod(r*10+d, base)                if result or q:                    result.append(q)            return result, r         def to_base(digits, base):            result = []            while digits:                digits, r = divide(digits, base)                result.append(r)            result.reverse()            return result         def count(digits):            digits_base = to_base(digits, b)            result = 0            for i in xrange(len(digits_base)):                if i-1 >= 0 and digits_base[i-1] > digits_base[i]:                    break                for j in xrange(digits_base[i-1] if i-1 >= 0 else 0, digits_base[i]):                    result = (result + nHr((b-1)-j+1, len(digits_base)-(i+1))) % MOD            else:                result = (result+1)%MOD            return result         digits_l = map(int, l)        decrease(digits_l)        digits_r = map(int, r)        return (count(digits_r) - count(digits_l)) % MOD 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗