Number of Beautiful Integers in the Range — LeetCode 2827 Python Solution

HardMathDynamic Programming
Problem
#2827
Reading time
4 min

The problem

You are given positive integers low, high, and k. A number is beautiful if it meets both of the following conditions: The count of even digits in the number is equal to the count of odd digits.

Example

Input
low = 10, high = 20, k = 3
Output
2
Explanation
There are 2 beautiful integers in the given range: [12,18].

Python solution

Python
class Solution:
    def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int:
        @cache
        def dfs(pos: int, mod: int, diff: int, lead: int, limit: int) -> int:
            if pos >= len(s):
                return mod == 0 and diff == 10
            up = int(s[pos]) if limit else 9
            ans = 0
            for i in range(up + 1):
                if i == 0 and lead:
                    ans += dfs(pos + 1, mod, diff, 1, limit and i == up)
                else:
                    nxt = diff + (1 if i % 2 == 1 else -1)
                    ans += dfs(pos + 1, (mod * 10 + i) % k, nxt, 0, limit and i == up)
            return ans

        s = str(high)
        a = dfs(0, 0, 10, 1, 1)
        dfs.cache_clear()
        s = str(low - 1)
        b = dfs(0, 0, 10, 1, 1)
        return a - b

Complexity

MeasureComplexity
TimeO((\log M)^2 \times k \times |\Sigma|)
SpaceO((\log M)^2 \times k), where M represents the size of the number high, and |\Sigma| represents the digit set auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 2827. Number of Beautiful Integers in the Range is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2827. Number of Beautiful Integers in the Range?
LeetCode 2827. Number of Beautiful Integers in the Range is rated Hard on LeetCode.
What is the time complexity of LeetCode 2827. Number of Beautiful Integers in the Range?
The Python solution on this page runs in O((\log M)^2 \times k \times |\Sigma|).
What is the space complexity of LeetCode 2827. Number of Beautiful Integers in the Range?
The Python solution on this page uses O((\log M)^2 \times k), where M represents the size of the number high, and |\Sigma| represents the digit set auxiliary space.
What topics does LeetCode 2827. Number of Beautiful Integers in the Range cover?
LeetCode 2827. Number of Beautiful Integers in the Range is tagged Math and Dynamic Programming on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview