Number of Beautiful Integers in the Range - Complete Solution Guide
Number of Beautiful Integers in the Range is LeetCode problem 2827, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 di
Detailed Explanation
The problem asks us to count the number of 'beautiful' integers within a given range [low, high]. A number is considered beautiful if it satisfies two conditions: 1) the number of even digits equals the number of odd digits in the integer, and 2) the integer is divisible by a given number 'k'. The inputs are low, high, and k, and the output is the count of beautiful numbers in the range [low, high]. Constraints include 0 < low <= high <= 10^9 and 0 < k <= 20.
Solution Approach
The provided solution employs a digit DP approach to efficiently count beautiful integers. It uses recursion with memoization (lru_cache in Python, Integer[][][][][] in Java, vector<vector<...>> dp in C++) to store and reuse results of overlapping subproblems. The core idea is to build numbers digit by digit, keeping track of the current position, whether the digits chosen so far match the given number (tight constraint), the count of even and odd digits seen so far, and the remainder when divided by 'k'. By exploring all possible digits for each position, we can determine the number of beautiful integers that can be formed.
Step-by-Step Algorithm
- Step 1: Define a recursive function `dp(pos, is_tight, even_count, odd_count, rem, is_leading_zero)` that represents the number of beautiful integers formed from position `pos` onwards, given the current state (is_tight, even/odd digit counts, remainder, and leading zero status).
- Step 2: The base case for the recursion is when `pos == n` (we have considered all digits). If `even_count == odd_count` and `rem == 0` and `is_leading_zero` is false, it means we have found a beautiful number, so return 1; otherwise, return 0.
- Step 3: In the recursive step, iterate through all possible digits (0 to 9) or up to the digit at the current position of input number if `is_tight` is true.
- Step 4: Update the state variables for each digit: `is_tight` becomes true if the current digit equals the digit in input number at the current position. Increment `even_count` or `odd_count` based on whether the digit is even or odd. Update the remainder `rem` by multiplying the previous remainder by 10 and adding the current digit, modulo `k`.
- Step 5: Recursively call the `dp` function with the updated state and add the result to the `res` variable. The case of leading zeros requires special handling to prevent incorrect counts.
- Step 6: Memoize the result of the `dp` function to avoid redundant calculations.
- Step 7: The `solve` function converts the input integer to a string and calls the `dp` function starting from the leftmost digit (position 0).
- Step 8: Finally, to find the number of beautiful integers in the range [low, high], compute `solve(str(high)) - solve(str(low - 1))`, where `str` represents the conversion of an integer to a string.
Key Insights
- Insight 1: The problem can be solved efficiently using dynamic programming (specifically, digit DP) due to the overlapping subproblems arising from considering digits individually and the constraints on 'k'.
- Insight 2: We can decompose the problem into counting beautiful numbers up to 'high' and then subtracting the count of beautiful numbers up to 'low - 1'. This simplifies the range counting problem to a prefix counting problem.
- Insight 3: Handling the 'tight' constraint (ensuring the generated numbers don't exceed the upper bound) and leading zeros are crucial aspects of digit DP, as they affect the state transitions and valid counts.
Complexity Analysis
Time Complexity: O(n*k*n^2)
Space Complexity: O(n*k*n^2)
Topics
This problem involves: Math, Dynamic Programming.
Companies
Asked at: Infosys.