-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0254-factor-combinations.js
More file actions
36 lines (34 loc) · 922 Bytes
/
0254-factor-combinations.js
File metadata and controls
36 lines (34 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 254. Factor Combinations
* https://leetcode.com/problems/factor-combinations/
* Difficulty: Medium
*
* Numbers can be regarded as the product of their factors.
*
* For example, 8 = 2 x 2 x 2 = 2 x 4.
*
* Given an integer n, return all possible combinations of its factors. You may return the
* answer in any order.
*
* Note that the factors should be in the range [2, n - 1].
*/
/**
* @param {number} n
* @return {number[][]}
*/
var getFactors = function(n) {
const result = [];
findFactors(n, 2, []);
return result;
function findFactors(currentNum, start, combination) {
for (let i = start; i * i <= currentNum; i++) {
if (currentNum % i === 0) {
const nextNum = currentNum / i;
if (nextNum >= i && nextNum < currentNum) {
result.push([...combination, i, nextNum]);
findFactors(nextNum, i, [...combination, i]);
}
}
}
}
};