-
Notifications
You must be signed in to change notification settings - Fork 1
/
0040.cpp
38 lines (31 loc) · 947 Bytes
/
0040.cpp
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
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
map<int, set<multiset<int>>> dp;
dp[0] = {{}};
for (auto x : candidates)
{
auto next = dp;
for (auto& level : dp)
for (auto base : level.second)
{
int sum = 0;
for (auto n : base)
sum += n;
if (sum + x > target)
continue;
base.insert(x);
next[sum + x].insert(move(base));
}
dp = move(next);
}
vector<vector<int>> result;
for (auto base : dp[target])
{
vector<int> add({ base.begin(), base.end() });
result.push_back(add);
}
return result;
}
};