-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination-sum.h
54 lines (45 loc) · 1.44 KB
/
combination-sum.h
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
#ifndef COMBINATION_SUM_H_
#define COMBINATION_SUM_H_
#include <algorithm>
#include <deque>
#include <vector>
namespace solution {
std::vector<std::vector<int>> combinationSum(std::vector<int>& candidates,
int target) {
// kind of backtracking
// Runtime: 24 ms, faster than 35.04% of C++ online submissions for Combination Sum.
// Memory Usage: 14.9 MB, less than 38.60% of C++ online submissions for Combination Sum.
//
std::sort(candidates.begin(), candidates.end());
if (target < candidates.front()) return {};
if (target == candidates.front()) return {{target}};
std::vector<std::vector<int>> result;
std::deque<std::pair<int, std::vector<int>>> deque;
for (auto& candidate : candidates) {
if (candidate < target) {
deque.push_back({candidate, {candidate}});
} else if (candidate == target) {
result.push_back({candidate});
break;
}
}
while (!deque.empty()) {
auto& p = deque.front();
for (auto& candidate : candidates) {
auto sum = p.first + candidate;
if ((p.second.empty() || p.second.back() <= candidate) && sum <= target) {
std::vector<int> v{p.second};
v.push_back(candidate);
if (sum < target) {
deque.push_back({sum, v});
} else {
result.push_back(v);
}
}
}
deque.pop_front();
}
return result;
}
} // namespace solution
#endif // !COMBINATION_SUM_H_