forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0040-combination-sum-ii.js
39 lines (31 loc) · 1.1 KB
/
0040-combination-sum-ii.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
35
36
37
38
39
/**
* https://leetcode.com/problems/combination-sum-ii/
* Time O(2^N) | Space O(N)
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
candidates.sort((a, b) => a - b)
return dfs(candidates, target)
};
const dfs = (candidates, target, index = 0, combination = [], combinations = []) => {
const isBaseCase = target < 0;
if (isBaseCase) return combinations;
const isTarget = target === 0;
if (isTarget) {
if (combination.length) combinations.push(combination.slice());
return combinations
}
for (let i = index; i < candidates.length; i++) {
const isDuplicate = (index < i) && (candidates[i - 1] === candidates[i]);
if (isDuplicate) continue;
backTrack(candidates, target, i, combination, combinations);
}
return combinations;
}
const backTrack = (candidates, target, i, combination, combinations) => {
combination.push(candidates[i])
dfs(candidates, (target - candidates[i]), (i + 1), combination, combinations)
combination.pop()
}