Simple Recursive with O(n) time & O(1) space


  • 0
    G
    class Solution {
    public:
        int fib(int a, int b, int n){
            if(n<=2) return n;
            return a + fib(b,a+b,n-1);
        }
        int climbStairs(int n) {
            return fib(1,2,n);
        }
    };

Log in to reply
 

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