-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum2.js
More file actions
69 lines (59 loc) · 2.18 KB
/
combinationSum2.js
File metadata and controls
69 lines (59 loc) · 2.18 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[[1,1,6],[1,2,5],[1,7],[2,6]]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5
Output:
[[1,2,2],[5]]
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
/**
* Finds all unique combinations of numbers that sum up to the target.
* Each number in candidates can be used only once.
*
* @param {number[]} candidates - The array of numbers (can include duplicates).
* @param {number} target - The sum we need to reach.
* @return {number[][]} - A list of unique combinations.
*/
var combinationSum2 = function (candidates, target) {
let result = [];
candidates.sort((a, b) => a - b); // Sort to handle duplicates efficiently
findCombinations(result, [], candidates, target, 0);
return result;
};
/**
* Helper function to perform backtracking.
*
* @param {number[][]} result - Stores all unique combinations.
* @param {number[]} tempList - Tracks the current combination being built.
* @param {number[]} candidates - The sorted list of numbers.
* @param {number} totalLeft - Remaining sum needed to reach the target.
* @param {number} startIndex - The current position in the array.
*/
function findCombinations(result, tempList, candidates, totalLeft, startIndex) {
if (totalLeft === 0) {
result.push([...tempList]); // Found a valid combination
return;
}
for (let i = startIndex; i < candidates.length; i++) {
// Skip duplicate elements to avoid duplicate combinations
if (i > startIndex && candidates[i] === candidates[i - 1]) continue;
if (candidates[i] > totalLeft) break; // Optimization: No need to check further
tempList.push(candidates[i]); // Include current number
findCombinations(
result,
tempList,
candidates,
totalLeft - candidates[i],
i + 1
);
tempList.pop(); // Backtrack to explore other possibilities
}
}