This is a Fibonacci question


  • 0

    this is actually a Fibonacci question. so the solution is the same with Fibonnacci

    public class Solution {
        public int climbStairs(int n) {
            // this is a fibnacci question
            if(n < 2) { return 1; }
            int[] res = new int[n+1];
            res[0]=1;
            res[1]=1;
            for(int i = 2; i <= n; i++) {
                res[i] = res[i-1] + res[i-2];
            }
            return res[n];
        }
    }
    

  • 2
    Z

    You can reduce space by only keeping track of two previous results.


  • 0

    @zhengping good idea :)


Log in to reply
 

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