forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0039-combination-sum.cpp
37 lines (29 loc) · 1021 Bytes
/
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(2^target)
Space: O(target)
*/
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
std::vector<int> curr;
std::vector<std::vector<int>> res;
helper(candidates, target, 0, curr, res);
return res;
}
private:
void helper(std::vector<int>& cands, int target, int i, std::vector<int>& curr, std::vector<std::vector<int>>& res) {
if (i >= cands.size() || target < 0)
return;
if (target == 0) {
res.push_back(curr);
return;
}
curr.push_back(cands[i]);
helper(cands, target - cands[i], i, curr, res);
curr.pop_back();
helper(cands, target, i + 1, curr, res);
}
};