-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalan.cpp
45 lines (38 loc) · 903 Bytes
/
catalan.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
class Solution
{
public:
//Function to find the nth catalan number.
/*
int solve(int n, vector<int> &dp)
{
const int mod = 1e9 + 7;
//code here
if(n<=1)
return 1;
if(dp[n]!=-1)
return (dp[n]%mod);
int ans=0;
//every i node is the root node
for(int i=1;i<=n;i++)
{
ans += solve(i-1,dp) * solve(n-i,dp);
}
return dp[n]=ans%mod;
}
*/
int findCatalan(int n)
{
vector<long long > dp(n+1, 0);
const int mod = 1e9 + 7;
dp[0] = 1;
dp[1] = 1;
// i -> number of nodes
for(int i=2; i<=n; i++){
// j -> root node
for(int j=1; j<=i; j++){
dp[i] = ( dp[i] + ( dp[j-1] * dp[i-j] ) %mod ) %mod ;
}
}
return dp[n];
}
};