My java solution2(dp top down)


  • 0
    C
     public class Solution {
     public int climbStairs(int n) {
    	int [] steps = new int[n+1];
    	if(n <=2)return n;
    	//base case
    	steps[0] = 0;steps[1] = 1;steps[2] = 2;
    	return steps(steps,n);
    }
    
    int steps(int[] steps,int n)
    {
    	if(steps[n] != 0)
    	{
    		return steps[n];
    	}
    	
    	else
    	{ steps[n] = steps(steps,n-1)+ steps(steps,n-2);
    		return steps[n];
    	}
    }
    

    }


Log in to reply
 

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