Introduction
The Climbing Stairs problem is one of the most commonly asked coding interview questions for beginners. It is a perfect example to understand recursion, memoization, and dynamic programming (DP).
In this article, we will break down the problem step by step and explore multiple approaches—from brute force recursion to an optimized space-efficient solution.
Link of Problem: LeetCode – Climbing Stairs
Problem Statement
You are climbing a staircase that has n steps.
- Each time, you can either climb 1 step or 2 steps.
- The goal is to calculate the total number of distinct ways to reach the top.
Example
Input:
n = 2
Output:
2
Explanation:
- 1 + 1
- 2
Input:
n = 3
Output:
3
Explanation:
- 1 + 1 + 1
- 1 + 2
- 2 + 1
Key Insight
To reach step n, there are only two possibilities:
- From step
n-1(taking 1 step) - From step
n-2(taking 2 steps)
So, the recurrence relation becomes:
This is identical to the Fibonacci sequence, making this problem a classic DP question.
Approach 1: Recursive Solution (Brute Force)
Idea
Break the problem into smaller subproblems:
- Count ways to reach
n-1 - Count ways to reach
n-2 - Add both results
Code
Complexity
- Time Complexity: O(2^n)
- Space Complexity: O(n)
Drawback
This solution recalculates the same subproblems multiple times, leading to Time Limit Exceeded (TLE) for larger values.
Approach 2: Recursion with Memoization (Top-Down DP)
Idea
To optimize recursion, store already computed results using a HashMap.
- Avoid repeated calculations
- Convert exponential time into linear time
Code
Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
Why It Works
Memoization ensures each subproblem is solved only once, making recursion efficient and practical.
Approach 3: Dynamic Programming (Bottom-Up)
Idea
Instead of recursion, build the solution iteratively:
- Use an array
dp[] - Store results for each step
- Build from smaller values to larger ones
Code
Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
Approach 4: Optimal Solution (Space Optimized)
Idea
We only need the last two values instead of the whole array.
Code
Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
Key Takeaways
- The problem follows a Fibonacci-like pattern
- Brute force recursion is simple but inefficient
- Memoization converts recursion into an efficient solution
- Dynamic programming avoids recursion completely
- Space optimization reduces memory usage to constant space
When This Problem Is Asked
This question is frequently asked in:
- Coding interviews (product-based companies)
- Data Structures & Algorithms exams
- Online coding platforms
It evaluates:
- Problem-solving ability
- Understanding of recursion
- Optimization skills
Conclusion
The Climbing Stairs problem is a foundational example for learning dynamic programming. Starting with recursion and improving it using memoization and iterative DP demonstrates how to optimize algorithms effectively.
Understanding this pattern will help solve many similar problems related to sequences and decision-making.
Frequently Asked Questions (FAQs)
1. Is this problem related to Fibonacci?
Yes, the recurrence relation is exactly the same as the Fibonacci sequence.
2. Why does recursion fail for large inputs?
Because it recalculates the same values repeatedly, leading to exponential time complexity.
3. What is the best approach?
The space-optimized approach is the most efficient with O(n) time and O(1) space.




