Climbing Stairs

https://leetcode.com/problems/climbing-stairs/description/?envType=study-plan-v2&id=dynamic-programming

class Solution {
    int cache[];
    public int climbStairs(int n) {
        cache = new int[n+1];
        return solve(n);
    }
    public int solve(int n)
    {
        if(n <= 1)
        {
            return 1;
        }
        if(cache[n] > 0)
        {
            return cache[n];
        }
        int ways = solve(n-1) + solve(n-2);
        cache[n] = ways;
        return ways;
    }
}

Related Post