-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
51 lines (48 loc) · 1.24 KB
/
Solution.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
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
public:
// Follows the sequence of Catalan Numbers, which has two rules:
// 1. "" is in the Set
// 2. if a and b are in the Set, then (a)b is in the set.
// Essentially, where f(n) refers to the set where each element has n pairs of
// parentheses:
// f(0) = ""
// f(1) = (f(0))f(0)
// f(2) = (f(0))f(1), (f(1))f(0)
// f(3) = (f(0))f(2), (f(1))f(1), (f(2))f(0)
// ...
// f(n) = (f(0))f(n-1), (f(1))f(n-2), ..., (f(n-1))f(0)
vector<string> generateParenthesis(int n) {
vector<vector<string>> dp(n + 1);
dp[0] = vector<string>(1, "");
for (int i = 1; i <= n; ++i) {
// For every combination of j, (i - 1 - j)
for (int j = 0; j < i; ++j) {
for (string const& a : dp[j]) {
for (string const& b : dp[i - j - 1]) {
dp[i].push_back('(' + a + ')' + b);
}
}
}
}
return dp[n];
}
};