Climbing Stairs Problem in DSA Rust - 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.
fn climb_stairs(n: i32) -> i32 {
if n <= 2 {
return n;
}
let mut first = 1;
let mut second = 2;
for _ in 3..=n {
let third = first + second;
first = second;
second = third;
}
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, adding two numbers each time.
- How many times: It runs (n - 2) times, once for each stair from 3 up to n.
As the number of stairs increases, the loop runs more times, adding one operation per stair after the second.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 8 additions |
| 100 | 98 additions |
| 1000 | 998 additions |
Pattern observation: The number of operations grows roughly in a straight line as n increases.
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 because of recursion."
[OK] Correct: This code uses a simple loop and stores only two previous results, so it runs quickly in a straight line, not exploding exponentially.
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 changed the problem to allow steps of 1, 2, or 3 at a time? 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 reach the top by taking either 1 or 2 steps at a time.Step 2: Identify what is being counted
We count all possible 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 distinct sequences = total ways [OK]
- Confusing number of ways with minimum steps
- Counting only 1-step moves
- Ignoring 2-step moves
n: i32 returning an i32?Solution
Step 1: Check parameter and return types
The input is number of steps as i32, and output is number of ways as i32.Step 2: Match function signature
Onlyfn climb_stairs(n: i32) -> i32matches input and output types correctly.Final Answer:
fn climb_stairs(n: i32) -> i32 -> Option DQuick Check:
Input and output types match function signature [OK]
- Using wrong return type like String
- Passing input as String instead of i32
- Missing return type or extra parameters
n = 4?
fn climb_stairs(n: i32) -> i32 {
if n <= 2 {
return n;
}
let mut first = 1;
let mut second = 2;
let mut ways = 0;
for _ in 3..=n {
ways = first + second;
first = second;
second = ways;
}
ways
}
fn main() {
println!("{}", climb_stairs(4));
}Solution
Step 1: Trace initial values and loop
n=4, first=1, second=2. Loop runs for i=3 and i=4. - i=3: ways=1+2=3, first=2, second=3 - i=4: ways=2+3=5, first=3, second=5Step 2: Return final ways value
After loop, ways=5 is returned and printed.Final Answer:
5 -> Option CQuick Check:
Ways for 4 steps = 5 [OK]
- Returning wrong initial value
- Off-by-one errors in loop
- Mixing first and second variables
fn climb_stairs(n: i32) -> i32 {
if n == 1 {
return 1;
}
let mut first = 1;
let mut second = 2;
let mut ways = 0;
for i in 2..n {
ways = first + second;
first = second;
second = ways;
}
ways
}Solution
Step 1: Identify the issue with base case n=2
For n=2, if not triggered, loop 2..2 empty, returns ways=0 (wrong, should be 2). second initialized to 2.Step 2: Observe the update logic
Each loop sets second to the new ways value (nth ways). After loop (or none), second is correct: initial 2 for n=2, updated for larger n.Final Answer:
Return second instead of ways at the end -> Option AQuick Check:
second holds nth ways after shifts [OK]
- Loop runs too short or too long
- Wrong initial if condition
- Returning wrong variable
Solution
Step 1: Understand the new step options
Now steps can be 1, 2, or 3 at a time, so ways to reach step i depend on previous 3 steps.Step 2: Update recurrence relation and base cases
Use dp[i] = dp[i-1] + dp[i-2] + dp[i-3]. Base cases: dp[0]=1 (one way to stand still), dp[1]=1, dp[2]=2.Final Answer:
Use dp[i] = dp[i-1] + dp[i-2] + dp[i-3] with base cases dp[0]=1, dp[1]=1, dp[2]=2 -> Option AQuick Check:
Sum of last 3 dp values for each step [OK]
- Multiplying dp values instead of adding
- Ignoring 3-step moves
- Using recursion without memoization causing slowness
Ready to prove it?
Pass the test and this pattern moves unknown
