-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40-combinationSum-2.cpp
More file actions
48 lines (41 loc) · 1.21 KB
/
Copy path40-combinationSum-2.cpp
File metadata and controls
48 lines (41 loc) · 1.21 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
class Solution {
public:
using int2=pair<char, char>;
int n;
vector<vector<int>> result;
void backtrack(int i, vector<int>& subset, vector<int2>& nWm, int target) {
if (target == 0) {
result.push_back(subset);
return;
}
if (i == n || target < 0)
return;
auto [x, m] = nWm[i];
int j0=min((int)m, target/x);// this can save computations
for (int j = 0; j <= j0; j++) {
// Add j x's to
int sz=subset.size();
subset.resize(sz+j);
fill(subset.begin()+sz, subset.end(), x);
backtrack(i+1, subset, nWm, target-j*x);
//backtracking
subset.resize(sz);
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
char freq[51]={0}, xMax=0;
for(char x: candidates){
freq[x]++;
xMax=max(xMax, x);
}
vector<int2> nWm;
for (char x=1; x<=xMax; x++) {
char f=freq[x];
if (f>0) nWm.push_back({x, f});
}
n=nWm.size();
vector<int> subset;
backtrack(0, subset, nWm, target);
return result;
}
};