Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Beautiful Numbers

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3490" 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 two positive integers, l and r. A positive integer is called beautiful if the product of its digits is divisible by the sum of its digits. Return the count of beautiful numbers between l and r, inclusive. Example 1: Input: l = 10, r = 20 Output: 2 Explanation: The beautiful numbers in the range are 10 and 20. Example 2: Input: l = 1, r = 15 Output: 10 Explanation: The beautiful numbers in the range are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Constraints: 1 <= l <= r < 109

Explanation

Here's the breakdown of the solution and the Python code:

  • High-Level Approach:

    • Use dynamic programming (specifically, digit DP) to efficiently count beautiful numbers. The DP state will represent the progress of building a number digit by digit.
    • The DP state will track the current product of digits, the sum of digits, the current position in the number, and a flag indicating whether we are still within the range defined by the upper bound.
    • Handle the divisibility check carefully by iterating through possible sums of digits and checking if the product is divisible by the sum.
  • Complexity:

    • Runtime Complexity: O(9 9 9 9 81 log(r)), which simplifies to roughly O(r^0.5) because the number of digits is bounded by log(r) and the maximum sum and product are also constrained. Storage Complexity: O(9 9 9 9 81 log(r)), same as runtime.

Code

    def count_beautiful_numbers(l, r):
    def solve(n):
        s = str(n)
        length = len(s)
        dp = {}

        def digit_dp(index, product, sum_digits, is_tight):
            if index == length:
                if sum_digits == 0:
                    return 0
                return (product % sum_digits == 0)

            if (index, product, sum_digits, is_tight) in dp:
                return dp[(index, product, sum_digits, is_tight)]

            limit = int(s[index]) if is_tight else 9
            ans = 0
            for digit in range(limit + 1):
                new_product = product * digit if (product != 0 or digit != 0) else digit # Handle initial zero case
                new_sum = sum_digits + digit
                new_is_tight = is_tight and (digit == int(s[index]))
                ans += digit_dp(index + 1, new_product, new_sum, new_is_tight)

            dp[(index, product, sum_digits, is_tight)] = ans
            return ans

        return digit_dp(0, 0, 0, True)

    return solve(r) - solve(l - 1)

More from this blog

C

Chatmagic blog

2894 posts