Accepted Java solution, time complexity O(n), space complexity O(n)


  • 0
    B

    public class Solution {
    public int climbStairs(int n) {

        if(n == 0 || n == 1){
            return 1;
        } else if(n == 2){
             return 2;
        }
        
        else{
        
        int[] step = new int[n+1];
        step[0] = 0;
        step[1] = 1;
        step[2] = 2;
        
       
             
       int i = 3;
       while(i < n ){
           step[i] = step[i-1] + step[i-2];
           i++;
           }
           
       step[n] = step[n-1] + step[n-2];
        
        
        return step[n];
        }    
        
    }
    

    }


Log in to reply
 

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