-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path39.cpp
55 lines (46 loc) · 1.9 KB
/
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
//https://leetcode.com/problems/combination-sum/
//Difficulty Level: Medium
//Tags: Array, Backtracking
//since we have to print all, and not just count or max or min, we won't use DP but backtracking
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());
//Note that we don't have duplicates in the array, else we had to delete the duplicates here bcoz
//we can already take each element multiple times, so duplicate elements don't make a difference
int index = candidates.size()-1;
while(index >=0 && candidates[index] > target)
{
index--;
}
vector<int> v;
vector<vector<int>> res; //stores result
backtrack(candidates, target, 0, index, v, res);
return res;
}
void backtrack(vector<int> candidates, int target, int curr_sum, int index, vector<int> v, vector<vector<int>>& res)
{
if(curr_sum == target) //if the sum of elements of v add up to target, push v to result vector
{
res.push_back(v);
return;
}
//check all the elements <= target - curr_sum
for(int i=index; i>=0; i--)
{
curr_sum += candidates[i];
if(curr_sum > target) //don't include the element if sum is exceeding the target
{
curr_sum -= candidates[i];
continue;
}
v.push_back(candidates[i]);
//backtrack to find rest of the elements of v
//note that we have passed 'i' and not 'i+1' since we could include the same element any no. of times
backtrack(candidates, target, curr_sum, i, v, res);
curr_sum -= candidates[i];
v.pop_back();
}
}
};