There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top.

Stairs

Consider the example shown in diagram. The value of n is 3. There are 3 ways to reach the top. The diagram is taken from Easier Fibonacci puzzles

[ad type=”banner”]

More Examples:

Input: n = 1
Output: 1
There is only one way to climb 1 stair

Input: n = 2
Output: 2
There are two ways: (1, 1) and (2)

Input: n = 4
Output: 5
(1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)

We can easily find recursive nature in above problem. The person can reach n’th stair from either (n-1)’th stair or from (n-2)’th stair. Let the total number of ways to reach n’t stair be ‘ways(n)’. The value of ‘ways(n)’ can be written as following.

    ways(n) = ways(n-1) + ways(n-2)

The above expression is actually the expression for Fibonacci numbers, but there is one thing to notice, the value of ways(n) is equal to fibonacci(n+1).

ways(1) = fib(2) = 1
ways(2) = fib(3) = 2
ways(3) = fib(4) = 3

So we can use function for fibonacci numbers to find the value of ways(n).

[ad type=”banner”]

Following is C implementation of the above idea.

[pastacode lang=”c” manual=”%2F%2F%20A%20C%20program%20to%20count%20number%20of%20ways%20to%20reach%20n’t%20stair%20when%0A%2F%2F%20a%20person%20can%20climb%201%2C%202%2C%20..m%20stairs%20at%20a%20time.%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20simple%20recursive%20program%20to%20find%20n’th%20fibonacci%20number%0Aint%20fib(int%20n)%0A%7B%0A%20%20%20if%20(n%20%3C%3D%201)%0A%20%20%20%20%20%20return%20n%3B%0A%20%20%20return%20fib(n-1)%20%2B%20fib(n-2)%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20number%20of%20ways%20to%20reach%20s’th%20stair%0Aint%20countWays(int%20s)%0A%7B%0A%20%20%20%20return%20fib(s%20%2B%201)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main%20()%0A%7B%0A%20%20int%20s%20%3D%204%3B%0A%20%20printf(%22Number%20of%20ways%20%3D%20%25d%22%2C%20countWays(s))%3B%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”C Program” highlight=”” provider=”manual”/]

Output:
Number of ways = 5
The time complexity of the above implementation is exponential (golden ratio raised to power n). It can be optimized to work in O(Logn) time using the previously discussed Fibonacci function optimizations.

Generalization of the above problem
How to count number of ways if the person can climb up to m stairs for a given value m? For example if m is 4, the person can climb 1 stair or 2 stairs or 3 stairs or 4 stairs at a time.

We can write the recurrence as following.

ways(n, m) = ways(n-1, m) + ways(n-2, m) + … ways(n-m, m)
Following is C implementation of above recurrence.

[pastacode lang=”c” manual=”%2F%2F%20A%20C%20program%20to%20count%20number%20of%20ways%20to%20reach%20n’t%20stair%20when%0A%2F%2F%20a%20person%20can%20climb%20either%201%20or%202%20stairs%20at%20a%20time%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20recursive%20function%20used%20by%20countWays%0Aint%20countWaysUtil(int%20n%2C%20int%20m)%0A%7B%0A%20%20%20%20if%20(n%20%3C%3D%201)%0A%20%20%20%20%20%20%20%20return%20n%3B%0A%20%20%20%20int%20res%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%3C%3Dm%20%26%26%20i%3C%3Dn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20countWaysUtil(n-i%2C%20m)%3B%0A%20%20%20%20return%20res%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20number%20of%20ways%20to%20reach%20s’th%20stair%0Aint%20countWays(int%20s%2C%20int%20m)%0A%7B%0A%20%20%20%20return%20countWaysUtil(s%2B1%2C%20m)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main%20()%0A%7B%0A%20%20%20%20int%20s%20%3D%204%2C%20m%20%3D%202%3B%0A%20%20%20%20printf(%22Nuber%20of%20ways%20%3D%20%25d%22%2C%20countWays(s%2C%20m))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C Program2″ highlight=”” provider=”manual”/]

Output:

Number of ways = 5

The time complexity of above solution is exponential. It can be optimized to O(mn) by using dynamic programming. Following is dynamic programming based solution. We build a table res[] in bottom up manner.

[ad type=”banner”] [pastacode lang=”c” manual=”%2F%2F%20A%20C%20program%20to%20count%20number%20of%20ways%20to%20reach%20n’t%20stair%20when%0A%2F%2F%20a%20person%20can%20climb%201%2C%202%2C%20..m%20stairs%20at%20a%20time%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20recursive%20function%20used%20by%20countWays%0Aint%20countWaysUtil(int%20n%2C%20int%20m)%0A%7B%0A%20%20%20%20int%20res%5Bn%5D%3B%0A%20%20%20%20res%5B0%5D%20%3D%201%3B%20res%5B1%5D%20%3D%201%3B%0A%20%20%20%20for%20(int%20i%3D2%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20res%5Bi%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20for%20(int%20j%3D1%3B%20j%3C%3Dm%20%26%26%20j%3C%3Di%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20res%5Bi%5D%20%2B%3D%20res%5Bi-j%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20res%5Bn-1%5D%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20number%20of%20ways%20to%20reach%20s’th%20stair%0Aint%20countWays(int%20s%2C%20int%20m)%0A%7B%0A%20%20%20%20return%20countWaysUtil(s%2B1%2C%20m)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main%20()%0A%7B%0A%20%20%20%20int%20s%20%3D%204%2C%20m%20%3D%202%3B%0A%20%20%20%20printf(%22Nuber%20of%20ways%20%3D%20%25d%22%2C%20countWays(s%2C%20m))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C Program” highlight=”” provider=”manual”/]

Output:

Number of ways = 5
[ad type=”banner”]