My cpp solution. Dynamic top-down


  • 0
    I
    static int ans[100];
    
    class Solution {
    public:
        int climbStairs_hlpfn(int n){
            if(ans[n])
                return ans[n];
            ans[n]=climbStairs_hlpfn(n-1)+climbStairs_hlpfn(n-2);
            return ans[n];
        }
        
        int climbStairs(int n) {
            for (int i=0;i<100;++i)
                ans[i]=0;
            ans[1]=1;
            ans[2]=2;
            return climbStairs_hlpfn(n);
        }
    };

Log in to reply
 

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