-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40.combination-sum-ii.cc
More file actions
77 lines (61 loc) · 1.66 KB
/
Copy path40.combination-sum-ii.cc
File metadata and controls
77 lines (61 loc) · 1.66 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
#include <algorithm>
#include <vector>
#include "leetcode_common_struct.h"
using std::vector;
/// 12ms
/// Beats 15.98 % of users with C++
/// 12.33MB
/// Beats 21.39 % of users with C++
bool end_with_k_num(const std::vector<int> &vec, int num, int k) {
auto myk = k;
int i = vec.size() - 1;
while (i >= 0 && myk >= 0 && vec[i] == num)
--i, --myk;
return !myk;
}
vector<vector<int>> combinationSum2(vector<int> &candidates, int target) {
std::sort(candidates.begin(), candidates.end());
std::vector<std::vector<std::vector<int>>> sums(
target + 1, std::vector<std::vector<int>>());
int dup_cnt = 0;
for (auto i = 0; i < candidates.size(); ++i) {
if (candidates[i] > target) {
continue;
}
int num = candidates[i];
bool dup = i > 0 && candidates[i] == candidates[i - 1];
if (dup) {
++dup_cnt;
} else {
dup_cnt = 0;
}
for (int j = target - num; j >= 0; --j) {
if (sums[j].empty())
continue;
for (auto k = 0; k < sums[j].size(); ++k) {
if (dup && !end_with_k_num(sums[j][k], num, dup_cnt)) {
continue;
}
sums[j + num].push_back(sums[j][k]);
sums[j + num].back().push_back(num);
}
}
if (sums[num].empty() || sums[num][0].size() != 1) {
sums[num].insert(sums[num].begin(), {num});
}
}
return sums[target];
}
int main(int argc, char **argv) {
int target = std::atoi(argv[1]);
std::vector<int> candidates;
for (auto i = 2; i < argc; ++i) {
candidates.push_back(std::atoi(argv[i]));
}
auto ret = combinationSum2(candidates, target);
printf("[\n");
for (auto res : ret) {
PrintVect(res);
}
printf("]\n");
}