Catalan Number
The n
th Catalan number is given directly in terms of binomial coefficients by
// OJ: https://leetcode.com/problems/unique-binary-search-trees
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int numTrees(int n) {
long long ans = 1, i;
for (i = 1; i <= n; ++i) ans = ans * (i + n) / i;
return ans / i;
}
};
Problems
Related:
Reference
Last updated
Was this helpful?