forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique-binary-search-trees.cpp
47 lines (43 loc) · 993 Bytes
/
unique-binary-search-trees.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
// Time: O(n)
// Space: O(1)
// Catalan Number: C(2n, n) - C(2n, n - 1)
class Solution {
public:
/**
* @paramn n: An integer
* @return: An integer
*/
int numTrees(int n) {
if (n == 0) {
return 1;
}
return combination(2 * n, n) - combination(2 * n, n - 1);
}
int combination(const int n, const int k) {
long long count = 1;
// C(n, k) = (n) / 1 * (n - 1) / 2 ... * (n - k + 1) / k
for (int i = 1; i <= k; ++i) {
count = count * (n - i + 1) / i;
}
return count;
}
};
// Time: O(n^2)
// Space: O(n)
class Solution2 {
public:
/**
* @paramn n: An integer
* @return: An integer
*/
int numTrees(int n) {
vector<int> num(n + 1, 0);
num[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
num[i] += num[j] * num[i - 1 - j];
}
}
return num[n];
}
};