3490. Count Beautiful Numbers LeetCode Solution
In this guide, you will get 3490. Count Beautiful Numbers LeetCode Solution with the best time and space complexity. The solution to Count Beautiful Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Beautiful Numbers solution in C++
- Count Beautiful Numbers solution in Java
- Count Beautiful Numbers solution in Python
- Additional Resources
Problem Statement of Count Beautiful Numbers
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
Complexity Analysis
- Time Complexity: O(n \cdot 2^3 \cdot (n \cdot 9) \cdot (3 \cdot n \cdot 2 \cdot n \cdot 9 \cdot 9) \cdot 10) = O(n^4), where n = \log r
- Space Complexity: O(n \cdot 2^3 \cdot (n \cdot 9) \cdot (3 \cdot n \cdot 2 \cdot n \cdot 9 \cdot 9))
3490. Count Beautiful Numbers LeetCode Solution in C++
class Solution {
public:
int beautifulNumbers(int l, int r) {
return count(to_string(r), 0, /*tight=*/true, /*isLeadingZero=*/true,
/*hasZero=*/false, /*sum=*/0, /*prod=*/1, {}) -
count(to_string(l - 1), 0, /*tight=*/true, /*isLeadingZero=*/true,
/*hasZero=*/false, /*sum=*/0, /*prod=*/1, {});
}
private:
int count(const string& s, int i, bool tight, bool isLeadingZero,
bool hasZero, int sum, int prod, unordered_map<string, int>&& mem) {
if (i == s.length()) {
if (isLeadingZero)
return 0;
return (hasZero || prod % sum == 0) ? 1 : 0;
}
const string key = hash(i, tight, isLeadingZero, hasZero, sum, prod);
if (!isLeadingZero && hasZero && !tight)
return mem[key] = pow(10, s.length() - i);
if (const auto it = mem.find(key); it != mem.end())
return it->second;
int res = 0;
const int maxDigit = tight ? s[i] - '0' : 9;
for (int d = 0; d <= maxDigit; ++d) {
const bool nextTight = tight && (d == maxDigit);
const bool nextIsLeadingZero = isLeadingZero && d == 0;
const bool nextHasZero = !nextIsLeadingZero && d == 0;
const int nextProd = nextIsLeadingZero ? 1 : prod * d;
res += count(s, i + 1, nextTight, nextIsLeadingZero, nextHasZero, sum + d,
nextProd, std::move(mem));
}
return mem[key] = res;
}
string hash(int i, bool tight, bool isLeadingZero, bool hasZero, int sum,
int prod) {
return to_string(i) + "_" + (tight ? "1" : "0") + "_" +
(isLeadingZero ? "1" : "0") + "_" + (hasZero ? "1" : "0") + "_" +
to_string(sum) + "_" + to_string(prod);
}
};
/* code provided by PROGIEZ */
3490. Count Beautiful Numbers LeetCode Solution in Java
class Solution {
public int beautifulNumbers(int l, int r) {
return count(String.valueOf(r), 0, /*tight=*/true, /*isLeadingZero=*/true,
/*hasZero=*/false, /*sum=*/0, /*prod=*/1, new HashMap<>()) -
count(String.valueOf(l - 1), 0, /*tight=*/true, /*isLeadingZero=*/true,
/*hasZero=*/false, /*sum=*/0, /*prod=*/1, new HashMap<>());
}
private int count(final String s, int i, boolean tight, boolean isLeadingZero, boolean hasZero,
int sum, int prod, Map<String, Integer> mem) {
if (i == s.length()) {
if (isLeadingZero)
return 0;
return (hasZero || prod % sum == 0) ? 1 : 0;
}
final String key = hash(i, tight, isLeadingZero, hasZero, sum, prod);
if (!isLeadingZero && hasZero && !tight) {
final int val = (int) Math.pow(10, s.length() - i);
mem.put(key, val);
return val;
}
if (mem.containsKey(key))
return mem.get(key);
int res = 0;
final int maxDigit = tight ? s.charAt(i) - '0' : 9;
for (int d = 0; d <= maxDigit; ++d) {
final boolean nextTight = tight && (d == maxDigit);
final boolean nextIsLeadingZero = isLeadingZero && d == 0;
final boolean nextHasZero = !nextIsLeadingZero && d == 0;
final int nextProd = nextIsLeadingZero ? 1 : prod * d;
res += count(s, i + 1, nextTight, nextIsLeadingZero, nextHasZero, sum + d, nextProd, mem);
}
mem.put(key, res);
return res;
}
private String hash(int i, boolean tight, boolean isLeadingZero, boolean hasZero, int sum,
int prod) {
return i + "_" + (tight ? "1" : "0") + "_" + (isLeadingZero ? "1" : "0") + "_" +
(hasZero ? "1" : "0") + "_" + sum + "_" + prod;
}
}
// code provided by PROGIEZ
3490. Count Beautiful Numbers LeetCode Solution in Python
class Solution:
def beautifulNumbers(self, l: int, r: int) -> int:
@functools.lru_cache(None)
def dp(
s: str,
i: int,
tight: bool,
isLeadingZero: bool,
hasZero: bool,
sum: int,
prod: int,
) -> int:
if i == len(s):
if isLeadingZero:
return 0
return 1 if hasZero or prod % sum == 0 else 0
if not isLeadingZero and hasZero and not tight:
return 10 ** (len(s) - i)
res = 0
maxDigit = int(s[i]) if tight else 9
for d in range(maxDigit + 1):
nextTight = tight and (d == maxDigit)
nextIsLeadingZero = isLeadingZero and d == 0
nextHasZero = not nextIsLeadingZero and d == 0
nextProd = 1 if nextIsLeadingZero else prod * d
res += dp(s, i + 1, nextTight, nextIsLeadingZero,
nextHasZero, sum + d, nextProd)
return res
return (dp(str(r), 0, tight=True, isLeadingZero=True, hasZero=False, sum=0, prod=1) -
dp(str(l - 1), 0, tight=True, isLeadingZero=True, hasZero=False, sum=0, prod=1))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.