-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinationSum.cpp
40 lines (34 loc) · 916 Bytes
/
CombinationSum.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
#include<bits/stdc++.h>
#include "../utilities.h"
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& arr, int target) {
vector<vector<int>> ans;
vector<int> ds;
size_t s = arr.size();
function<void(int, int)> findCombinations;
findCombinations = [&](int idx, int target) mutable -> void {
if (idx == s) return;
if (target == 0) {
ans.push_back(ds);
return;
}
if(arr[idx] <= target) {
ds.push_back(arr[idx]);
findCombinations(idx, target - arr[idx]);
ds.pop_back();
}
findCombinations(idx + 1, target);
};
findCombinations(0, target);
return ans;
}
};
int main() {
vector<int> test1{2, 3, 6, 7};
Solution s;
vector<vector<int>> ans = s.combinationSum(test1, 7);
cout<< ans;
return 0;
}