Skip to content

Commit 5e26512

Browse files
committed
Add solution #625
1 parent 3b7bfb2 commit 5e26512

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@
582582
622|[Design Circular Queue](./solutions/0622-design-circular-queue.js)|Medium|
583583
623|[Add One Row to Tree](./solutions/0623-add-one-row-to-tree.js)|Medium|
584584
624|[Maximum Distance in Arrays](./solutions/0624-maximum-distance-in-arrays.js)|Medium|
585+
625|[Minimum Factorization](./solutions/0625-minimum-factorization.js)|Medium|
585586
628|[Maximum Product of Three Numbers](./solutions/0628-maximum-product-of-three-numbers.js)|Easy|
586587
629|[K Inverse Pairs Array](./solutions/0629-k-inverse-pairs-array.js)|Hard|
587588
630|[Course Schedule III](./solutions/0630-course-schedule-iii.js)|Hard|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 625. Minimum Factorization
3+
* https://leetcode.com/problems/minimum-factorization/
4+
* Difficulty: Medium
5+
*
6+
* Given a positive integer num, return the smallest positive integer x whose multiplication
7+
* of each digit equals num. If there is no answer or the answer is not fit in 32-bit signed
8+
* integer, return 0.
9+
*/
10+
11+
/**
12+
* @param {number} num
13+
* @return {number}
14+
*/
15+
var smallestFactorization = function(num) {
16+
if (num < 2) return num;
17+
18+
const digits = [];
19+
for (let divisor = 9; divisor > 1; divisor--) {
20+
while (num % divisor === 0) {
21+
digits.push(divisor);
22+
num /= divisor;
23+
}
24+
}
25+
26+
if (num > 1) return 0;
27+
28+
let result = 0;
29+
for (const digit of digits.reverse()) {
30+
result = result * 10 + digit;
31+
if (result > 2**31 - 1) return 0;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)