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 when the stairs increase?
Analyze the time complexity of the following code snippet.
int climbStairs(int n) {
if (n <= 2) return n;
int first = 1, second = 2, third = 0;
for (int i = 3; i <= n; i++) {
third = first + second;
first = second;
second = third;
}
return second;
}
This code calculates how many distinct ways there are to climb n stairs, taking 1 or 2 steps at a time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 3 to n.
- How many times: It runs (n - 2) times, once for each stair from 3 up to n.
Each new stair adds one more loop iteration, so the work grows steadily as n grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 8 (loop runs 8 times) |
| 100 | 98 (loop runs 98 times) |
| 1000 | 998 (loop runs 998 times) |
Pattern observation: The number of operations grows linearly with the number of stairs.
Time Complexity: O(n)
This means the time to find the number of ways grows in direct proportion to the number of stairs.
[X] Wrong: "Because the problem involves Fibonacci numbers, the time complexity is exponential."
[OK] Correct: This code uses a simple loop and stores only needed values, so it runs in linear time, not exponential.
Understanding this problem's time complexity shows you can spot efficient solutions and avoid slow recursive traps, a key skill in interviews.
"What if we changed the code to use simple recursion without memoization? 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 all possible ways to climb to the top using steps of size 1 or 2.Step 2: Identify what is being counted
We count distinct sequences of 1-step and 2-step moves that sum to n steps.Final Answer:
The total number of distinct ways to reach the top -> Option BQuick Check:
Counting ways = total distinct sequences [OK]
- Confusing number of ways with minimum steps
- Thinking about maximum step size instead of counting ways
- Ignoring the step size options
Solution
Step 1: Identify base cases for climbing stairs
For 1 step, ways = 1 (only one way). For 2 steps, ways = 2 (1+1 or 2).Step 2: Match base cases with options
int ways[1] = 1; int ways[2] = 2; correctly sets ways[1] = 1 and ways[2] = 2.Final Answer:
int ways[1] = 1; int ways[2] = 2; -> Option AQuick Check:
Base cases: ways[1]=1, ways[2]=2 [OK]
- Setting ways[0] incorrectly
- Confusing ways for 1 and 2 steps
- Using zero for ways[1]
n = 4?int climbStairs(int n) {
if (n <= 2) return n;
int a = 1, b = 2, c;
for (int i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
printf("%d", climbStairs(4));Solution
Step 1: Trace the loop for n=4
Initial: a=1, b=2 i=3: c=1+2=3, a=2, b=3 i=4: c=2+3=5, a=3, b=5Step 2: Return value after loop
Return b=5, which is the number of ways to climb 4 steps.Final Answer:
5 -> Option CQuick Check:
Ways(4) = 5 [OK]
- Returning a instead of b
- Off-by-one errors in loop
- Incorrect initial values
int climbStairs(int n) {
int ways[n+1];
ways[1] = 1;
ways[2] = 2;
for (int i = 3; i <= n; i++) {
ways[i] = ways[i-1] + ways[i-2];
}
return ways[n];
}Solution
Step 1: Check array initialization
Array ways is declared but ways[0] is not initialized, which may cause undefined behavior if accessed.Step 2: Analyze usage of ways[0]
Though ways[0] is not used in the loop, some compilers or logic might expect it initialized; best practice is to initialize ways[0] = 0.Final Answer:
Array ways is not initialized for ways[0] -> Option AQuick Check:
Uninitialized ways[0] can cause issues [OK]
- Ignoring ways[0] initialization
- Assuming no undefined behavior without ways[0]
- Starting loop incorrectly
Solution
Step 1: Understand step size options
Since steps can be 1, 2, or 3, ways to reach step i include ways to reach i-1, i-2, and i-3.Step 2: Formulate recurrence and base cases
ways[i] = ways[i-1] + ways[i-2] + ways[i-3]; base cases: ways[0]=1 (one way to stand still), ways[1]=1, ways[2]=2.Final Answer:
Use ways[i] = ways[i-1] + ways[i-2] + ways[i-3] with base cases ways[0]=1, ways[1]=1, ways[2]=2 -> Option DQuick Check:
Sum previous 3 ways for steps 1,2,3 [OK]
- Multiplying ways instead of adding
- Ignoring the 3-step option
- Using wrong base cases
Ready to prove it?
Pass the test and this pattern moves unknown
