Given a positive integer num, return the smallest positive integer x whose multiplication of each digit equals num. If there is no answer or the answer is not fit in 32-bit signed integer, return 0.
Example 1:
Input: num = 48 Output: 68
Example 2:
Input: num = 15 Output: 35
Constraints:
1 <= num <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a target number and need to find the smallest number whose digits multiply together to give you that target. The brute force approach is to test every single whole number, starting from one and going upwards, to see if its digits multiply to match your target.
Here's how the algorithm would work step-by-step:
def min_factorization(target_number):
# Handle edge case where target is 1 since digits of 1 multiply to 1
if target_number == 1:
return 1
candidate_number = 1
while True:
digit_product = 1
temp_number = candidate_number
while temp_number > 0:
digit_product *= temp_number % 10
temp_number //= 10
# Stop checking when we find the first number whose digits match target
if digit_product == target_number:
return candidate_number
candidate_number += 1To find the smallest number that multiplies to give our target, we should work backwards by finding its largest possible single-digit factors. By building the number from right to left using the biggest digits first, we naturally guarantee that the final number will be as small as possible.
Here's how the algorithm would work step-by-step:
class Solution:
def smallest_factorization(self, target_integer: int) -> int:
# Single digit numbers factorize into themselves immediately.
if target_integer < 10:
return target_integer
factor_collection = []
current_target = target_integer
# Extract largest possible single digits from right to left.
for current_digit in range(9, 1, -1):
while current_target % current_digit == 0:
factor_collection.append(str(current_digit))
current_target //= current_digit
# If target cannot be fully reduced, factorization is impossible.
if current_target > 1:
return 0
# Reverse digits to form the smallest possible number.
result_number = int("".join(reversed(factor_collection)))
# Ensure result fits within standard 32-bit signed integer limits.
if result_number > 2147483647:
return 0
return result_number| Case | How to Handle |
|---|---|
| Input a is equal to 1 | Return 1 immediately since 1 is the smallest positive integer whose digits multiply to 1. |
| Input a is a prime number greater than 9 | Return 0 because no single digit can represent a prime greater than 9 and it cannot be factored further. |
| Input a exceeds the 32-bit signed integer limit during reconstruction | Check the accumulated result against the maximum integer limit before each multiplication step to prevent overflow. |
| Input a contains prime factors greater than 9 | Return 0 since digits only range from 2 to 9 and factors like 11 or 13 cannot be formed by a single digit. |
| Input a is already a single digit between 2 and 9 | Return the number itself as it represents the minimum valid integer. |
| Multiple digits can form the factors | Greedily extract factors from 9 down to 2 and prepend them to build the smallest possible number. |