VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-ways-reach-nth-stair/

⇱ Climbing stairs to reach the top - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Climbing stairs to reach the top

Last Updated : 3 May, 2026

Given n stairs, and a person standing at the ground wants to climb stairs 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 at the top.

Examples:

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

Input: n = 2
Output: 2
Explanation: There are two ways to reach 2th stair: {1, 1} and {2}.

Input: n = 4
Output: 5
Explanation: There are five ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2}, {2, 1, 1}, {1, 2, 1} and {2, 2}.

[Naive Approach] Using Recursion ā€“ O(2^n) Time and O(n) Space

In this approach, we observe that to reach the nth stair, one can come either from the (nāˆ’1)th or (nāˆ’2)th stair. Similarly, to reach the (nāˆ’1)th stair, we must know the ways to reach the (nāˆ’2)th and (nāˆ’3)rd stairs, and so on. This way we can observe that this can be done using recursion.

Note: The above recurrence relation is same as Fibonacci numbers.


Output
5

[Better Approach - 1] Using Top-Down DP (Memoization) ā€“ O(n) Time and O(n) Space

In this approach, we notice that the same subproblems are being solved multiple times.
For example, countWays(n) calls countWays(n-1) and countWays(n-2), and countWays(n-1) again calls countWays(n-2) and countWays(n-3).
Here, countWays(n-2) is computed more than once. To avoid this redundant work, we can store the results of subproblems in a dp array and reuse them whenever needed.

Here is the visualization of overlapping subproblems for n=4.

šŸ‘ frame_3291

Output
5

[Better Approach - 2] Using Bottom-Up DP (Tabulation) – O(n) Time and O(n) Space

In this approach, we are iteratively calculating the answers from the ground up, beginning with the smallest subproblems - the first two stairs. Using these base values, we then find the answers for subsequent stairs one by one. Each new value is computed using the results of the previous two (which have already been calculated) and stored for further calculations.


Output
5

[Space Optimised] Using Space Optimized DP – O(n) Time and O(1) Space

In this approach, we observe that in order to calculate value for a particular state, we only need answers for previous two states. Hence, we store the results of the previous two states, and as we move forward, we update them to compute the next values. This means keeping track of the last two states and using them to calculate the next one.


Output
5

[Expected Approach] Using Matrix Exponentiation – O(logn) Time and O(1) Space

In this approach, we observe that the problem is similar to Fibonacci series. The traditional way is to repeatedly add two previous numbers using loop or recursive call. But, with matrix exponentiation, we can calculate Fibonacci numbers much faster by working with matrices. There's a special matrix (transformation matrix) that represents how Fibonacci numbers work.

To know more about how to calculate Fibonacci number using Matrix Exponentiation, refer to this post.


Output
5
Comment