-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
47 lines (46 loc) · 1.32 KB
/
program.cpp
File metadata and controls
47 lines (46 loc) · 1.32 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
#include "../include/pre.h"
#include <iterator>
#include <deque>
#include <set>
std::deque<int> cur;
vector<vector<int>> rst;
void helper(std::vector<std::pair<int, size_t>>& candidateCount, 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 (auto i = candidateCount.begin() + used; i != candidateCount.end(); i++) {
if (i->second != 0) {
cur.push_back(i->first);
i->second--;
helper(candidateCount, used, target - i->first);
cur.pop_back();
i->second++;
}
used++;
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
{
cur = {}, rst = {};
std::sort(candidates.begin(), candidates.end());
vector<std::pair<int, size_t>> candidateCount;
int tmp = candidates[0] - 1;
for (auto n : candidates) {
if (tmp != n) candidateCount.emplace_back(n, 1);
else candidateCount.rbegin()->second++;
tmp = n;
}
helper(candidateCount, 0, target);
return rst;
}
int main()
{
//auto r = combinationSum2(vector<int>{10,1,2,7,6,1,5,}, 8);
auto r = combinationSum2(vector<int>{4,1,1,4,4,4,4,2,3,5}, 10);
for (auto& l : r) cout << l << endl;
return 0;
}