-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCombinationSum_39.cpp
99 lines (78 loc) · 3.18 KB
/
CombinationSum_39.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 39. Combination Sum
~ Link : https://leetcode.com/problems/combination-sum/
*/
class Solution {
public:
void combinationSumUtil(int idx, int target, vector<int>& nums, vector<int>& curr, vector<vector<int> >& ans) {
// If we have traversed all elements
if (idx == nums.size()) {
// If all considerations till now adds up to form given target,
// then the sequence in 'curr' is a valid combination
// if (target == 0)
// ans.emplace_back(curr);
return;
}
// If at any index we have formed our given sum,
// then the sequence in 'curr' is a valid combination
if (target == 0) {
ans.emplace_back(curr);
return;
}
// If current element is greater than target,
// then all all succeeding elements would be greater than target,
// as the given array has been sorted
if (nums[idx] > target) {
return;
}
// If current element is <= target, then
// ~ Case 1: Include current element
else if (nums[idx] <= target) {
curr.emplace_back(nums[idx]);
combinationSumUtil(idx, target - nums[idx], nums, curr, ans);
curr.pop_back();
}
// ~ Case 2: Dont include current element
combinationSumUtil(idx + 1, target, nums, curr, ans);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
// We sort the given array in non-decreasing order,
// so that once we reach an element greater than the current target,
// we can return, as because all succeeding elements would be greater than the current target.
sort(candidates.begin(), candidates.end());
vector<vector<int> > ans;
vector<int> curr;
combinationSumUtil(0, target, candidates, curr, ans);
return ans;
}
};
// class Solution {
// public:
// void combinationSumUtil(int idx, int target, vector<int>& nums, vector<int>& curr, vector<vector<int> >& ans) {
// if (idx == nums.size()) {
// if (target == 0)
// ans.emplace_back(curr);
// return;
// }
// /*
// If current element is <= target, then
// ~ Case 1: Include current element
// ~ Case 2: Dont include current element
// Else if, current element is > target, then
// ~ Case 1: Dont include current element
// */
// if (nums[idx] <= target) {
// curr.emplace_back(nums[idx]);
// combinationSumUtil(idx, target - nums[idx], nums, curr, ans);
// curr.pop_back();
// }
// combinationSumUtil(idx + 1, target, nums, curr, ans);
// }
// vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
// vector<vector<int> > ans;
// vector<int> curr;
// combinationSumUtil(0, target, candidates, curr, ans);
// return ans;
// }
// };