Climbing Stairs Problem in DSA Javascript - 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 to climb increase with more stairs?
Analyze the time complexity of the following code snippet.
function climbStairs(n) {
if (n <= 2) return n;
let first = 1, second = 2;
for (let i = 3; i <= n; i++) {
let 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: A single for-loop that runs from 3 up to n.
- How many times: The loop runs (n - 2) times, which grows as n grows.
Each additional stair adds one more loop cycle, so the work grows steadily as n increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 8 loops |
| 100 | 98 loops |
| 1000 | 998 loops |
Pattern observation: The number of operations grows roughly in a straight line with n.
Time Complexity: O(n)
This means the time to find the number of ways grows directly in proportion to the number of stairs.
[X] Wrong: "Because the problem looks like Fibonacci, it must take exponential time."
[OK] Correct: This code uses a simple loop and stores only two previous results, so it runs in linear time, not exponential.
Understanding this problem's time complexity shows you can spot when a recursive idea can be turned into a fast loop, a key skill in coding interviews.
"What if we allowed steps of size 1, 2, or 3? How would the time complexity change?"
Practice
Given n stairs, 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 to the top using steps of size 1 or 2.Step 2: Identify what is being counted
We count all possible sequences of 1-step and 2-step moves that sum to n stairs.Final Answer:
The total number of distinct ways to reach the top -> Option AQuick Check:
Counting sequences = total ways [OK]
- Thinking it's about minimum steps
- Confusing number of ways with step size
- Ignoring the 2-step option
Solution
Step 1: Identify the function name and parameters
The problem requires one input: the number of stairs n. The function name should be descriptive and camelCase is standard in JavaScript.Step 2: Check each option
function climbStairs(n) {} uses camelCase and one parameter n, which is correct. function climb_stairs(n) {} uses snake_case which is uncommon in JS. function climbStairs(n, m) {} has an extra parameter m which is unnecessary. function climbStairs() {} has no parameters, so it cannot accept n.Final Answer:
function climbStairs(n) {} -> Option CQuick Check:
Correct function name and parameter [OK]
- Using snake_case instead of camelCase
- Adding unnecessary parameters
- Omitting the parameter for stairs
function climbStairs(n) {
if (n <= 2) return n;
let a = 1, b = 2, temp;
for (let i = 3; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
console.log(climbStairs(4));Solution
Step 1: Understand the base cases and loop
For n=1, returns 1; for n=2, returns 2. For n=4, loop runs from i=3 to 4.Step 2: Trace the loop iterations
i=3: temp = 1+2=3, a=2, b=3
i=4: temp = 2+3=5, a=3, b=5
Return b=5.Final Answer:
5 -> Option BQuick Check:
Ways for 4 stairs = 5 [OK]
- Returning a instead of b
- Miscounting loop iterations
- Confusing base case returns
function climbStairs(n) {
if (n == 1) return 1;
let a = 1, b = 2;
for (let i = 2; i <= n; i++) {
let temp = a + b;
a = b;
b = temp;
}
return b;
}Solution
Step 1: Analyze the loop range
The loop runs from i=2 to n inclusive. For n=2, loop runs once, but base cases expect no loop for n=2.Step 2: Identify correct loop start
Loop should start at i=3 to avoid extra iteration and match base cases. Starting at i=2 causes one extra iteration.Final Answer:
Loop starts at wrong index causing extra iteration -> Option AQuick Check:
Loop index must start at 3 [OK]
- Starting loop at 2 instead of 3
- Misunderstanding base cases
- Returning wrong variable
function climbStairs(n) {
if (n <= 1) return 1;
let dp = [1, 1, 2];
for (let i = 3; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-2] + dp[i-3];
}
return dp[n];
}Solution
Step 1: Understand the problem extension
Now steps can be 1, 2, or 3. The number of ways to reach step i is sum of ways to reach i-1, i-2, and i-3.Step 2: Check code logic and base cases
dp array starts with dp[0]=1 (1 way to stand still), dp[1]=1, dp[2]=2 (ways to climb 2 steps). Loop starts at i=3 and sums previous three dp values correctly.Final Answer:
This code correctly counts ways with 1, 2, or 3 steps -> Option DQuick Check:
Sum of last 3 dp values for each step [OK]
- Not updating base cases for 3-step
- Starting loop at wrong index
- Forgetting to sum three previous dp values
Ready to prove it?
Pass the test and this pattern moves unknown
