-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path70_combination-sum.cpp
47 lines (41 loc) · 1.23 KB
/
70_combination-sum.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
// cpp-blind-75/70_combination-sum.cpp
/**
* Date : 14-Aug-2023
* Repo : https://github.com/ankitsamaddar/
*
* Problem : Combination Sum
* Difficulty: 🟡Medium
*
* Leetcode 0039 : https://leetcode.com/problems/combination-sum
* Lintcode 0135 : https://www.lintcode.com/problem/135
*/
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
// call dfs to do sum
dfs(0, {}, 0, target, candidates);
return res;
}
private:
vector<vector<int>> res;
void dfs(int i, vector<int> cur, int total, int target, vector<int>& candidates) {
sort(candidates.begin(), candidates.end());
// if total is equal to target
// push the current combination array
if (total == target) {
res.push_back(cur);
return;
}
// do dfs for the numbers starting at i
for (int j = i; j < candidates.size(); j++) {
// break if adding current number to total exceeds target
if (total + candidates[j] > target) break;
// push the current number and do sum using dfs
cur.push_back(candidates[j]);
int sum = total + candidates[j];
dfs(j, cur, sum, target, candidates);
// pop out if returned
cur.pop_back();
}
}
};