forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0039-combination-sum.cpp
37 lines (32 loc) · 1.09 KB
/
0039-combination-sum.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
/*
Given distinct int array & a target, return list of all unique combos that sum to target
Ex. candidates = [2,3,6,7] target = 7 -> [[2,2,3],[7]]
Backtracking, generate all combo sums, push/pop + index checking to explore new combos
Time: O(n^target)
Space: O(target)
*/
class Solution {
public:
vector<vector<int>> combinationSum(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++) {
curr.push_back(candidates[i]);
dfs(candidates, target, sum + candidates[i], i, curr, result);
curr.pop_back();
}
}
};