Skip to content

70.ClimbingStairs

Problem Statement
  • You are climbing a staircase. It takes n steps to reach the top.
  • Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Code
class Solution {
public:

    int solve(int n,vector<int>& dp){
        if(n <= 1)
            return 1;
        if(dp[n] != -1)
            return dp[n];
        return dp[n] = solve(n-1,dp) + solve(n-2,dp);
    }

    int climbStairs(int n) {
        vector<int> dp(n+1,-1);
        dp[0] = 1;
        dp[1] = 1;
        return solve(n,dp);
    }
};