-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination Sum II
42 lines (32 loc) Β· 929 Bytes
/
Combination Sum II
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
41
42
//User function Template for C++
class Solution{
public:
vector<vector<int>> ans;
void solve(vector<int>& arr, int index, vector<int>& path ,long long sum, int k)
{
if(sum >= k)
{
if(sum == k)
ans.push_back(path);
return;
}
if(index >= arr.size())
{
return;
}
path.push_back(arr[index]);
solve(arr, index + 1, path, sum + arr[index], k);
path.pop_back();
while(index + 1 < arr.size() && arr[index] == arr[index + 1])
index++;
solve(arr, index + 1, path, sum, k);
}
vector<vector<int>> CombinationSum2(vector<int> arr,int n,int k)
{
sort(arr.begin(), arr.end());
vector<int> path;
long long sum = 0;
solve(arr, 0, path, sum, k);
return ans;
}
};