-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40. Combination Sum II.cpp
More file actions
29 lines (24 loc) · 1012 Bytes
/
40. Combination Sum II.cpp
File metadata and controls
29 lines (24 loc) · 1012 Bytes
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
class Solution {
public:
void combination(int index, int arrSize, int currSum, int target, vector<int>& ds, vector<vector<int>>& ans, vector<int> & arr) {
if(currSum > target) return;
if(currSum == target){
ans.push_back(ds);
return;
}
for(int i = index; i < arrSize; i++){
if(i > index && arr[i] == arr[i-1]) continue; // skip duplicates
if(currSum + arr[i] > target) break; // pruning
ds.push_back(arr[i]);
combination(i + 1, arrSize, currSum + arr[i], target, ds, ans, arr); // i+1 because each element can be used once
ds.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> ds;
sort(candidates.begin(), candidates.end()); // sort to handle duplicates and pruning
combination(0, candidates.size(), 0, target, ds, ans, candidates);
return ans;
}
};