-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0040-combination-sum-ii.cpp
40 lines (35 loc) · 1.22 KB
/
0040-combination-sum-ii.cpp
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
/*
Given array & a target, find all unique combos that sum to target, nums can only be used once
Ex. candidates = [10,1,2,7,6,1,5], target = 8 -> [[1,1,6],[1,2,5],[1,7],[2,6]]
Backtracking, generate all combo sums, push/pop + index checking to explore new combos
Time: O(2^n)
Space: O(n)
*/
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int> curr;
vector<vector<int>> result;
dfs(candidates, target, 0, 0, curr, result);
return result;
}
private:
void dfs(vector<int>& candidates, int target, int sum, int start, vector<int>& curr, vector<vector<int>>& result) {
if (sum > target) {
return;
}
if (sum == target) {
result.push_back(curr);
return;
}
for (int i = start; i < candidates.size(); i++) {
if (i > start && candidates[i] == candidates[i - 1]) {
continue;
}
curr.push_back(candidates[i]);
dfs(candidates, target, sum + candidates[i], i + 1, curr, result);
curr.pop_back();
}
}
};