-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from Anmol55555/master
nth Catalan Number Solution Added
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Program's_Contributed_By_Contributors/Dynamic Programming/nth Catalan Number.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
|
||
cpp_int solve(int n, vector<cpp_int> &dp) | ||
{ | ||
if(n == 0 || n == 1) | ||
return 1; | ||
|
||
if(dp[n] != -1) | ||
return dp[n]; | ||
|
||
cpp_int ans = 0; | ||
for(int i=0; i<=(n-1); i++) | ||
{ | ||
ans += solve(i, dp) * solve(n-1-i, dp); | ||
} | ||
|
||
return dp[n] = ans; | ||
} | ||
|
||
cpp_int findCatalan(int n) | ||
{ | ||
vector<cpp_int> dp(n+1, -1); | ||
|
||
return solve(n, dp); | ||
} | ||
|
||
int main() | ||
{ | ||
cpp_int n; | ||
cin>>n; | ||
|
||
cout<<"The "<<n<<"th Catalan Number is "<<findCatalan(n)<<endl; | ||
} | ||
|
||
|