Coding Exercise – Climbing Stairs – Fibonacci Numbers – C++ – Online Judge


Question: You are climbing stairs. It takes steps to get to the top. Each time, you can take 1 step or 2 steps. How many distinct ways to get to the top?

Problem Descriptionhttp://oj.leetcode.com/problems/climbing-stairs/

This problem may look difficult at first, but when you think for a second, you might find it so easy. The answer is Fibonacci Numbers (see here, here)

Well, the original Fibonacci Numbers are defined as sequences:

latex Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages

For this problem, we just have to modify a little bit:

tex_c4ec2575b1c9bae6f84cbbf914ebaef8 Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages and tex_28e2067593116d6b7df5a56dcdcd6dc8 Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages because for only 1 step, there is only 1 distinct way and for 2 steps, we have two solutions: 1 + 1 or 2.

The rest is as Fibonacci series. The tex_8a106cadf41fb2c79bf8c045b66e8cf3 Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages because from the current position, we are possibly from previous two positions, F(n-1) and F(n-2).

You can use recursion, but this can be solved by a more effective iterative solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    int climbStairs(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        // f(n) = f(n - 1) + f(n - 2)
        int a = 1, b = 2, c = a + b;
        for (int i = 3; i <= n; i ++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
};
class Solution {
public:
    int climbStairs(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        // f(n) = f(n - 1) + f(n - 2)
        int a = 1, b = 2, c = a + b;
        for (int i = 3; i <= n; i ++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
};

Or you prefer a more straightforward recursive function:

1
2
3
4
5
6
7
8
class Solution {
public:
    int climbStairs(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        return climbStairs(n - 1) + climbStairs(n - 2);
    }
};
class Solution {
public:
    int climbStairs(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        return climbStairs(n - 1) + climbStairs(n - 2);
    }
};

Note: The recursive will yield TIME LIMIT EXCEEDED on test 44.  That is because, the recursive function usually computes intermediate values many many times. For example, tex_832c0fc5c2744b588cc031d3b1e4cc0e Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages , tex_fdf335d3ab46da70384f41341699bee9 Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages . tex_30c4007b2c77b99ae063ffb3f3df1fa5 Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge  algorithms beginner c / c++ implementation interview questions math programming languages  is computed twice. 

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
646 words
Last Post: Coding Exercise - First Missing Positive - C++ - Online Judge
Next Post: How to Check If Two Binary Trees are the Same?

The Permanent URL is: Coding Exercise – Climbing Stairs – Fibonacci Numbers – C++ – Online Judge

One Response

Leave a Reply