Climbing Stairs Problem in DSA C++ - Time & Space Complexity
We want to understand how the time needed to solve the climbing stairs problem changes as the number of stairs grows.
Specifically, how does the number of steps to calculate ways increase with more stairs?
Analyze the time complexity of the following code snippet.
int climbStairs(int n) {
if (n <= 2) return n;
return climbStairs(n - 1) + climbStairs(n - 2);
}
This code calculates the number of ways to climb n stairs by taking 1 or 2 steps at a time using simple recursion.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls to climbStairs for n-1 and n-2.
- How many times: Each call splits into two more calls until base cases, causing many repeated calls.
Each call creates two more calls except at the base, so the total calls grow very fast as n increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 177 calls |
| 20 | Over 21,000 calls |
| 30 | More than 2 million calls |
Pattern observation: The number of calls roughly doubles with each increase in n, growing exponentially.
Time Complexity: O(2^n)
This means the time needed grows very fast, doubling roughly with each extra stair.
[X] Wrong: "The recursion only calls once per step, so it should be O(n)."
[OK] Correct: Each call actually makes two calls, causing many repeated calculations and exponential growth.
Understanding this problem helps you see how recursion can explode in work and why using memory to save results is important.
"What if we add a memory array to save results and avoid repeated calls? How would the time complexity change?"
Practice
Given n steps, how many distinct ways can you climb to the top if you can take 1 or 2 steps at a time?Solution
Step 1: Understand the problem statement
The problem asks for the count of different ways to climb n steps taking 1 or 2 steps at a time.Step 2: Identify what is being calculated
We want the total number of distinct sequences of 1-step and 2-step moves that sum to n.Final Answer:
The total number of distinct ways to reach the top -> Option BQuick Check:
Count ways to climb = total distinct sequences [OK]
- Confusing number of ways with minimum steps
- Thinking about maximum steps instead of counting ways
- Ignoring the step size constraints
Solution
Step 1: Identify the return type
The function should return an integer count of ways, so return type is int.Step 2: Check the parameter list
We need the number of steps n as input, passed by value as int n.Final Answer:
int climbStairs(int n); -> Option CQuick Check:
Return type int and parameter int n [OK]
- Using void return type instead of int
- Missing the parameter n
- Passing n by reference unnecessarily
int climbStairs(int n) {
if (n <= 2) return n;
return climbStairs(n-1) + climbStairs(n-2);
}
int main() {
std::cout << climbStairs(4);
return 0;
}Solution
Step 1: Trace climbStairs(4) calls
climbStairs(4) = climbStairs(3) + climbStairs(2) climbStairs(3) = climbStairs(2) + climbStairs(1) = 2 + 1 = 3 climbStairs(2) = 2Step 2: Calculate final result
climbStairs(4) = 3 + 2 = 5Final Answer:
5 -> Option DQuick Check:
Ways for 4 steps = 5 [OK]
- Returning n instead of sum for n > 2
- Miscounting base cases
- Confusing return values for climbStairs(3)
int climbStairs(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
return climbStairs(n-1) * climbStairs(n-2);
}Solution
Step 1: Check base cases
Base cases for n=1 and n=2 are correct returning 1 and 2 respectively.Step 2: Analyze recursive return statement
It uses multiplication (*) instead of addition (+), which is wrong for counting ways.Final Answer:
Using multiplication instead of addition in recursion -> Option AQuick Check:
Recursion sums ways, not multiplies [OK]
- Multiplying instead of adding recursive calls
- Incorrect base cases for n=1 or n=2
- Passing parameter by reference unnecessarily
Solution
Step 1: Understand the step options
Since steps can be 1, 2, or 3, ways to reach step n = sum of ways to reach n-1, n-2, and n-3.Step 2: Define base cases and recurrence
Base cases: dp[0]=1 (one way to stand still), dp[1]=1, dp[2]=2. Recurrence: dp[n] = dp[n-1] + dp[n-2] + dp[n-3].Final Answer:
Use dp[n] = dp[n-1] + dp[n-2] + dp[n-3] with base dp[0]=1, dp[1]=1, dp[2]=2 -> Option AQuick Check:
Sum ways from last 3 steps with correct bases [OK]
- Multiplying ways instead of adding
- Ignoring dp[n-3] term
- Wrong base cases causing incorrect counts
Ready to prove it?
Pass the test and this pattern moves unknown
