Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Beautiful Integers in the Range

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2827" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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. The number is divisible by k. Return the number of beautiful integers in the range [low, high]. Example 1: Input: low = 10, high = 20, k = 3 Output: 2 Explanation: There are 2 beautiful integers in the given range: [12,18]. - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that: - 16 is not beautiful because it is not divisible by k = 3. - 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range. Example 2: Input: low = 1, high = 10, k = 1 Output: 1 Explanation: There is 1 beautiful integer in the given range: [10]. - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range. Example 3: Input: low = 5, high = 5, k = 2 Output: 0 Explanation: There are 0 beautiful integers in the given range. - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits. Constraints: 0 < low <= high <= 109 0 < k <= 20

Explanation

Here's the breakdown of the approach, complexity, and Python code:

  • High-Level Approach:

    • Use dynamic programming to count beautiful numbers less than or equal to a given number n.
    • The DP state will track the index (digit position), the count of even digits, the running remainder modulo k, and a boolean flag indicating if the current number is strictly less than n (to handle leading zeros).
    • Calculate the result by count_beautiful(high) - count_beautiful(low - 1).
  • Complexity:

    • Runtime Complexity: O(D * K * D), where D is the number of digits in high (at most 10), and K is the value of k (at most 20). Essentially O(1) since these are bounded by constants.
    • Storage Complexity: O(D * K * D) due to the DP table. Essentially O(1) since these are bounded by constants.

Code

    def solve():
    def count_beautiful(n, k):
        s = str(n)
        length = len(s)
        dp = {}

        def recurse(index, even_count, remainder, tight):
            if index == length:
                return 1 if even_count == length - even_count and remainder == 0 else 0

            if (index, even_count, remainder, tight) in dp:
                return dp[(index, even_count, remainder, tight)]

            limit = int(s[index]) if tight else 9
            ans = 0

            for digit in range(limit + 1):
                new_even_count = even_count + (1 if digit % 2 == 0 else 0)
                new_remainder = (remainder * 10 + digit) % k
                new_tight = tight and (digit == int(s[index]))
                ans += recurse(index + 1, new_even_count, new_remainder, new_tight)

            dp[(index, even_count, remainder, tight)] = ans
            return ans

        return recurse(0, 0, 0, True)

    low, high, k = map(int, input().split())

    print(count_beautiful(high, k) - count_beautiful(low - 1, k))

solve()

More from this blog

C

Chatmagic blog

2894 posts