forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_012.js
34 lines (29 loc) Β· 866 Bytes
/
problem_012.js
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
/**
* @param {*} numSteps
* @param {*} climbSet
* @return list of unique arrays
*/
const stairClimber = (numSteps, climbSet) => {
var uniqueList = [];
// Base case
if(numSteps < Math.min(...climbSet)) {
return uniqueList;
}
for(let step in climbSet) {
var currStep = climbSet[step];
if(numSteps === currStep) {
uniqueList.push(currStep);
} else if(numSteps > currStep) {
var childCombos = stairClimber(numSteps - currStep, climbSet);
for (let combo in childCombos) {
var currCombo = childCombos[combo];
uniqueList.push([currStep].concat(currCombo));
}
}
}
return uniqueList;
}
console.log(stairClimber(4, [1, 2]));
// [[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2]]
console.log(stairClimber(4, [1, 2, 3]));
// [[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 3], [2, 1, 1], [2, 2], [3, 1]]