-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path40-combination-sum-ii.cpp
More file actions
59 lines (46 loc) · 1.88 KB
/
40-combination-sum-ii.cpp
File metadata and controls
59 lines (46 loc) · 1.88 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
/*
40. Combination Sum II
Medium - 58.4%
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]]
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> combination;
// Sort to handle duplicates and enable early termination
sort(candidates.begin(), candidates.end());
backtrack(candidates, target, 0, combination, result);
return result;
}
private:
void backtrack(vector<int>& candidates, int target, int start,
vector<int>& combination, vector<vector<int>>& result) {
if (target == 0) {
result.push_back(combination);
return;
}
for (int i = start; i < candidates.size(); i++) {
// Skip duplicates: if current element is same as previous and we haven't used previous
if (i > start && candidates[i] == candidates[i - 1]) continue;
// Early termination if current candidate exceeds target
if (candidates[i] > target) break;
combination.push_back(candidates[i]);
// Use i + 1 to avoid reusing same element (each number used only once)
backtrack(candidates, target - candidates[i], i + 1, combination, result);
combination.pop_back(); // backtrack
}
}
};