-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCombination Sum III.cpp
55 lines (40 loc) · 1.42 KB
/
Combination Sum III.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Solution by Rahul Surana
***********************************************************
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations.
The list must not contain the same combination twice, and the combinations may be returned in any order.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
set<vector<int>> ans;
map<set<int>,bool> m;
void df(int g, set<int> x, int s, int k, int n){
if(s>n || x.size() >k || m[x]) return;
m[x] = true;
cout << s <<" -> ";
for(auto a: x) cout << a <<" ";
cout <<"\n";
if(s == n && x.size() == k){
ans.insert(vector<int>(x.begin(),x.end()));
return;
}
for(int i = g; i <= 9;i++){
if(!x.count(i)){
set<int> z(x.begin(),x.end());
z.insert(i);
df(i+1,z,s+i,k,n);
}
}
}
vector<vector<int>> combinationSum3(int k, int n) {
if(n > 9*5 || n < (k*(k+1)/2)) return vector<vector<int>>();
set<int> x;
df(1,x,0,k,n);
return vector<vector<int>>(ans.begin(),ans.end());
}
};