time O(n) space O(n)


  • 0
    X
    public int climbStairs(int n) {
        int[] dp = new int[n + 1]; // save all caculated values
        dp[0] = 1; // base case
        dp[1] = 1; // base case
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }
    

Log in to reply
 

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