625. Minimum Factorization π
DifficultyMedium
Description
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 - 1
Solutions
Solution 1
Thinking
We must write \(num\) as a product of digits \(2..9\) and form the smallest integer. Searching all factorizations is unnecessary.
Fewer digits and smaller high digits win, so divide by \(9\) down to \(2\) and write factors from the low end. If a remainder greater than \(1\) remains or the value overflows \(32\) bits, return \(0\).
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |