Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Solution:
public class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[n+1];
        dp[0] = 1;
        for(int i = 1; i<dp.length; i++){
            int op1 = 0, op2 = 0;
            if(i-1 >=0) op1 = dp[i-1];
            if(i-2 >=0) op2 = dp[i-2];
            dp[i] = op1+op2;
        }
        return dp[n];
    }
}

results matching ""

    No results matching ""