-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination_Sum2.cpp
More file actions
47 lines (42 loc) · 1.71 KB
/
Combination_Sum2.cpp
File metadata and controls
47 lines (42 loc) · 1.71 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
//O(N*M*M)
//O(1)
class Solution {
public:
void worker(int i, int target, vector<int> &count, vector<vector<int> >&result,
vector<int> &candidates, map<int, int>&number) {
if (i == candidates.size()) {
if (target == 0) {
vector<int> tmp;
for (int i = 0; i < count.size(); i++)
for (int j = 0; j < count[i]; j++)
tmp.push_back(candidates[i]);
result.push_back(tmp);
}
return;
}
for (int j = 0; j <= target / candidates[i] && j <= number[candidates[i]];
j++) {
count.push_back(j);
worker(i + 1, target - j * candidates[i], count, result, candidates,
number);
count.pop_back();
}
}
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
map<int, int> number;
vector<int> newCandidates;
for (int i = 0; i < candidates.size(); i++) {
if (number.find(candidates[i]) == number.end()) {
newCandidates.push_back(candidates[i]);
}
number[candidates[i]] = number[candidates[i]] + 1;
}
vector<vector<int> > result;
vector<int> count;
worker(0, target, count, result, newCandidates, number);
return result;
}
};