You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps
Example 2:
Input: n = 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
Constraints:
1 <= n <= 45The key idea behind #70 Climbing Stairs is recognizing that the number of ways to reach step n depends on how you reached the previous steps. From any step, you can either climb 1 or 2 stairs. This leads to the recurrence relation: the ways to reach step n equals the sum of ways to reach n-1 and n-2. This pattern closely resembles the Fibonacci sequence.
A straightforward recursive solution exists but results in repeated computations, making it inefficient. To optimize this, you can use memoization (top-down dynamic programming) to store previously computed results and avoid recomputation. Another approach is bottom-up dynamic programming, where you iteratively build solutions from smaller steps up to n.
For further optimization, since only the last two states are required, the DP array can be reduced to two variables, achieving O(1) space complexity while maintaining O(n) time complexity.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Naive Recursion | O(2^n) | O(n) |
| Dynamic Programming (Memoization / Tabulation) | O(n) | O(n) |
| Space Optimized DP | O(n) | O(1) |
NeetCode
Use these hints if you're stuck. Try solving on your own first.
To reach nth step, what could have been your previous steps? (Think about the step sizes)
This approach uses recursion to explore the different ways to reach the top. To optimize, we use memoization to store results of subproblems to avoid redundant calculations. This ensures that each subproblem is solved only once, dramatically improving efficiency.
Time Complexity: O(n), as each state is only computed once.
Space Complexity: O(n), due to the memoization array.
1function climbStairs(n) {
2 const memo = {};
3 function climb(n) {
4 if (n <= 2
This JavaScript solution leverages a helper function to recursively calculate stairs traversed, optimizing through memoization. The memo object stores previous results to avoid extra calculations.
This approach builds from the base up using a table to store results at each step. Starting with known base cases, each subsequent result is built by combining previous results. This eliminates the recursive overhead, making it a very efficient algorithm.
Time Complexity: O(n), since each value is calculated sequentially in a loop.
Space Complexity: O(n), for storing the results in the dp array.
1Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, Climbing Stairs is a very common beginner-level interview problem at companies like Google, Amazon, and Meta. It tests a candidate’s understanding of dynamic programming and recognizing Fibonacci-like patterns.
Dynamic programming structures such as arrays or simple variables are typically used. In memoization, a hash map or array stores previously computed results to avoid redundant recursion.
The optimal approach uses dynamic programming based on the Fibonacci pattern. Each step depends on the number of ways to reach the previous two steps. A space-optimized DP solution achieves O(n) time and O(1) space complexity.
The number of ways to reach step n equals the sum of ways to reach steps n-1 and n-2. This recurrence relation is identical to the Fibonacci sequence, making the problem a classic DP example.
This Java solution uses an array to perform a dynamic programming approach iteratively. Building upon basic cases, this method efficiently calculates the answer using an dp array to store results, avoiding recursive complexity.