-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.cpp
More file actions
42 lines (34 loc) · 1.03 KB
/
39.cpp
File metadata and controls
42 lines (34 loc) · 1.03 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
class Solution {
public:
struct data{
vector<int> v;
int c;
int sum;
};
vector<vector<int>> combinationSum(vector<int>& cs, int t) { //O(???)
vector<bool> pos(t+1); pos[0] = true;
for(int c : cs){
for(int i = c; i <= t; i++){
if(pos[i-c]) pos[i] = true;
}
}
vector<vector<int>> sol;
queue<data> q; q.push({{}, 0, t});
for(int i = 0; i < cs.size(); i++){
while(!q.empty() && q.front().c == i){
data d = q.front(); q.pop();
if(d.sum == 0){
continue;
}
d.c++;
while(d.sum >= 0 && pos[d.sum]){
q.push(d);
if(d.sum == 0) {sol.push_back(d.v); break;}
d.v.push_back(cs[i]);
d.sum -= cs[i];
}
}
}
return sol;
}
};