Climbing Stairs

Description

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?

Hide Company Tags: Adobe Apple

Hide Tags: Dynamic Programming

Thinking

Obviously, it is an Fibonacci Number Problem.

C++ Solution
Java Solution
class Solution {
public:
    int climbStairs(int n) {
        int prev = 0;
        int cur = 1;
        int result;

        for(int i = 1; i <= n; ++i){
            result = prev + cur;
            prev = cur;
            cur = result;
        }

        return cur;
    }
};
Template

The solution itself is an template for this category of problems.

results matching ""

    No results matching ""