DP solution in Java


  • 1
    G
    public int climbStairs(int n) {
        if(n < 2)
            return 1;
        int[] result = new int[n];
        result[0] = 1;
        result[1] = 2;
        
        for(int i = 2; i < n; i++)
            result[i] = result[i - 1] + result[i - 2];
            
        return result[n - 1];
    }

Log in to reply
 

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