Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Nth catalan number

Recursion :

def findCatalan(n):
    def recur(n):
        if n == 0 or n == 1:
            return 1
        res = 0
        for i in range(n):
            res += recur(i) * recur(n-i-1)
        return res
    return recur(n)

Top down :

def findCatalan(n): 
    def top_down(n,dp):
        if dp[n] != -1:
            return dp[n]
        res = 0
        for i in range(n):
            res += top_down(i,dp) * top_down(n-i-1,dp)
        dp[n] = res
        return dp[n]
    
    dp = [-1] * (n+1)
    dp[0] = 1
    dp[1] = 1
    return top_down(n,dp)

Bottom Up:

def findCatalan(n): 
    def bottom_up(n,dp):
        for i in range(2,n+1):
            for j in range(i):
                dp[i]  += dp[j] * dp[i-j-1]
    
    dp = [0] * (n+1)
    dp[0] = 1
    dp[1] = 1
    bottom_up(n,dp) # Calling function
    return dp[n]
Comment

nth catalan number

class Solution575
{
    static BigInteger[] catalan = new BigInteger[101];

    static{
        catalan[0] = new BigInteger("1");
        catalan[1] = new BigInteger("1");
    }

    //Function to find the nth catalan number.
    public static BigInteger findCatalan(int n)
    {
        //Your code here
        if(catalan[n]!=null){
            return catalan[n];
        }

        BigInteger total = new BigInteger("0");
        for(int i=0;i<n;i++){
            total = total.add(findCatalan(i).multiply(findCatalan(n-i-1)));
        }
        catalan[n] = total;
        return total;
    }
}
Comment

PREVIOUS NEXT
Code Example
Python :: flask flash The browser (or proxy) sent a request that this server could not understand. 
Python :: lower method in python 
Python :: programação funcional python - lambda 
Python :: get ip address 
Python :: += in python 
Python :: tables in jinja template 
Python :: are logN and (lognN) same 
Python :: discord py server.channels 
Python :: how to add badges to github repo 
Python :: creating a dictionary 
Python :: mean squared error in machine learning formula 
Python :: how to normalize scipy cross correlation 
Python :: join function in python 
Python :: string manipulation in python 
Python :: python write float with 2 decimals 
Python :: selenium wait until 
Python :: remove first element from list python 
Python :: private key 
Python :: combine 3 jupyter cells together 
Python :: else if 
Python :: cool python imports 
Python :: plotly express change legend labels 
Python :: curly braces in python 
Python :: print all objects in list python 
Python :: .sort python 
Python :: python iterator 
Python :: dictionary get all values 
Python :: python typing union 
Python :: search method in python 
Python :: python sort based on multiple keys 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =