-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
36 lines (34 loc) · 1015 Bytes
/
program.cpp
File metadata and controls
36 lines (34 loc) · 1015 Bytes
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
#include "../include/pre.h"
#include <deque>
std::deque<int> cur; //submission : 50673467
//std::vector<int> cur; //submission : 50673497
vector<vector<int>> rst;
void helper(const vector<int>& orderedCandidate, size_t used,int target)
{
if (target == 0) {
rst.emplace_back();
for (auto i : cur) rst.rbegin()->push_back(i);
return;
}
if (target < 0) return;
for (int i = used; i < orderedCandidate.size(); i++) {
cur.push_back(orderedCandidate[i]);
helper(orderedCandidate, used, target - orderedCandidate[i]);
cur.pop_back();
used++;
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
cur = {}, rst = {};
std::sort(candidates.begin(), candidates.end());
helper(candidates, 0, target);
return rst;
}
int main()
{
//auto r = combinationSum(vector<int>{1, 3, 4, 5, 6, }, 10);
auto r = combinationSum(vector<int>{1, 2, }, 4);
for (auto& l : r) cout << l << endl;
return 0;
}