My 0ms Java Result But not using fibonacci


  • 0
    S

    I didn't know that this problem can resolve with fibonacci numbers. I resolved the problem while calculating combination with dynamic programming. I surprised with result.Here is mycode:

    public int climbStairs(int n) {
    if(n<2)return n;

    	dp=new double[n+1];
    	dp[0]=1;
    	dp[1]=1;
    	double result=0;
       for(int i=0;i<=n/2;i++){
    	   int c=n-i;
    	   result+=calculateCombination(c, i);
       }
    
       return (int)result;
       
    }
    
    double dp[];
    private double calculateCombination(int n,int r){
    	return  (getFactorial(n)/(getFactorial(r)*getFactorial(n-r)));
    }
    private double getFactorial(int n){
    	if(dp[n]!=0) return dp[n];
    	dp[n]=n*getFactorial(n-1);
    	return dp[n];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.