-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.cpp
More file actions
37 lines (34 loc) · 1.1 KB
/
22.cpp
File metadata and controls
37 lines (34 loc) · 1.1 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
class Solution {
public:
vector<string> generateParenthesis(int n) { //O(4^n / √n), nth catalan number * n, 8 ms
vector<string> res;
addingpar(res, "", n, 0);
return res;
}
void addingpar(vector<string> &v, string str, int n, int m){
if(n==0 && m==0) {
v.push_back(str);
return;
}
if(m > 0){ addingpar(v, str+")", n, m-1); }
if(n > 0){ addingpar(v, str+"(", n-1, m+1); }
}
vector<string> slow(int n) { //O(n * n!), 30 ms
vector<int> a; for(int i = 1; i <= n; i++) a.push_back(i);
vector<string> ans; unordered_map<string, bool> seen;
do{
int maxA = -1;
int num = 0; string s = "";
for(int i = 0; i < n; i++){
maxA = max(maxA, a[i]);
while(num < maxA) {s += '('; num++;}
s += ')';
}
if(seen.count(s) < 1){
seen[s] = true;
ans.push_back(s);
}
} while(next_permutation(a.begin(), a.end()));
return ans;
}
};