-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinatonSum_Backtracking.cpp
More file actions
80 lines (75 loc) · 3.17 KB
/
CombinatonSum_Backtracking.cpp
File metadata and controls
80 lines (75 loc) · 3.17 KB
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
/******************************************************************************
* Given a set of candidate numbers (C) and a target number (T),
* find all unique combinations in C where the candidate numbers sums to T.
* The same repeated number may be chosen from C unlimited number of times.
*
* Note:
* All numbers (including target) will be positive integers.
* Elements in a combination (a1, a2, … , ak) must be in non-descending order.
* (ie, a1 = a2 = … = ak).
* The solution set must not contain duplicate combinations.
* For example, given candidate set 2,3,6,7 and target 7,
* A solution set is:
* [7]
* [2, 2, 3]
*
* The solution uses backtracking algorithm. We go through all potential
* solution subtrees until we get a combination or we rule out the subtree
* finding that possible combination can arise from that subtree.
* RUNTIME: O((n+m)!) where n is num elements in candidates and
* m is possible repetitions of the smallest candidate
* MEMORY: O(s) where is s is solution size. We store only possible
* combinations
*
* Compiled and tested on Leetcode online Judge
*
* Note: A dynamic programming approach is also possible, but it would consume
* a lot of space. A straightforward dynamic programming solution that
* I can think of would consume MEMORY of O(target*s) where is s is
* solution size and have a RUNTIME of O(target*n)
*****************************************************************************/
class Solution {
private:
void combinationSumRec(vector<int> &candidates, int target,
vector<vector<int> > &res, vector<int> &currVec,
int idx)
{
if (target < 0) {
/* Prune out this sub tree and backtrack */
return;
}
if (target == 0) {
/* We have got one of the combinations */
res.push_back(currVec);
} else {
while ((idx < candidates.size()) && (candidates[idx] <= target)) {
/* Go to a subtree with the hope of a possible combination and
include it in the currVec */
currVec.push_back(candidates[idx]);
/* Recursive in a subtree */
combinationSumRec(candidates, target-candidates[idx], res,
currVec, idx);
idx++;
/* Move to another potential subtree where a combination
can be found */
currVec.pop_back();
while((idx != candidates.size())
&& (candidates[idx] == candidates[idx-1])) {
/* Avoid duplicate solutions by pruning out same subtrees */
idx++;
}
}
}
}
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > res;
if (candidates.size() == 0) { return res; }
sort(candidates.begin(), candidates.end());
/* Current possible result in the potential solution tree */
vector<int> currVec;
/* Use recursion with backtracking */
combinationSumRec(candidates, target, res, currVec, 0);
return res;
}
};