-
Notifications
You must be signed in to change notification settings - Fork 257
/
k-sum-ii.cpp
36 lines (31 loc) · 923 Bytes
/
k-sum-ii.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
// Time: O(k * C(n, k))
// Space: O(k)
class Solution {
public:
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return a list of lists of integer
*/
vector<vector<int>> kSumII(vector<int> A, int k, int target) {
vector<vector<int>> ans;
vector<int> curr;
helper(A, k, 0, target, curr, ans);
return ans;
}
void helper(vector<int> A, int k, int start, int target, vector<int>& curr, vector<vector<int>> & ans) {
if (k < 0 || target < 0) {
return;
}
if (k == 0 && target == 0) {
ans.emplace_back(curr);
return;
}
for (int i = start; i <= A.size() - k; i++) {
curr.emplace_back(A[i]);
helper(A, k - 1, i + 1, target - A[i], curr, ans);
curr.pop_back();
}
}
};