Climbing Stairs Problem in DSA Go - Time & Space Complexity
We want to understand how the time needed to solve the climbing stairs problem grows as the number of stairs increases.
Specifically, how does the number of steps to calculate ways change when the stairs get longer?
Analyze the time complexity of the following code snippet.
func climbStairs(n int) int {
if n <= 2 {
return n
}
dp := make([]int, n+1)
dp[1], dp[2] = 1, 2
for i := 3; i <= n; i++ {
dp[i] = dp[i-1] + dp[i-2]
}
return dp[n]
}
This code calculates how many distinct ways there are to climb n stairs, taking 1 or 2 steps at a time, using a bottom-up approach.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that fills the dp array from 3 to n.
- How many times: It runs (n - 2) times, once for each stair from 3 up to n.
Each new stair requires one addition operation to find the number of ways.
| 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 compute the number of ways grows directly in proportion to the number of stairs.
[X] Wrong: "Because the problem uses recursion, it must be exponential time."
[OK] Correct: This code uses a loop with stored results (dynamic programming), so it only calculates each step once, avoiding repeated work.
Understanding this problem helps you see how to turn slow recursive solutions into fast ones using memory to save work, a key skill in many coding challenges.
"What if we changed the allowed steps from 1 or 2 to 1, 2, or 3 steps? 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 to the top by taking 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 AQuick Check:
Counting sequences of 1 or 2 steps = total ways [OK]
- Confusing number of ways with minimum steps
- Thinking about maximum steps allowed
- Counting skipped steps instead of ways
Solution
Step 1: Identify input and output types
The input is the number of steps (integer), output is the count of ways (integer).Step 2: Match function signature
Onlyfunc climbStairs(n int) intmatches input int and output int.Final Answer:
func climbStairs(n int) int -> Option DQuick Check:
Input int, output int = correct signature [OK]
- Using string instead of int for steps
- Missing input parameter
- Returning bool instead of int
n = 4?func climbStairs(n int) int {
if n <= 2 {
return n
}
a, b := 1, 2
for i := 3; i <= n; i++ {
a, b = b, a+b
}
return b
}
fmt.Println(climbStairs(4))Solution
Step 1: Trace initial values and loop
Start with a=1, b=2. Loop i=3 to 4:
i=3: a=2, b=3
i=4: a=3, b=5Step 2: Return final value
Return b=5 after loop ends.Final Answer:
5 -> Option CQuick Check:
Ways for 4 steps = 5 [OK]
- Returning a instead of b
- Off-by-one error in loop range
- Incorrect initial values
func climbStairs(n int) int {
if n == 1 {
return 1
}
a, b := 1, 2
for i := 2; i <= n; i++ {
a, b = b, a+b
}
return b
}Solution
Step 1: Analyze loop start and iterations
Loop starts at i=2 but initial values already cover steps 1 and 2, causing one extra iteration.Step 2: Correct loop start
Loop should start at i=3 to avoid counting extra ways.Final Answer:
Loop starts at wrong index causing extra iteration -> Option BQuick Check:
Loop index off by one causes wrong count [OK]
- Starting loop at 2 instead of 3
- Changing initial values incorrectly
- Returning wrong variable
Solution
Step 1: Understand the new step options
Now you can take 1, 2, or 3 steps at a time, so ways to reach step i depend on previous 3 steps.Step 2: Formulate recurrence relation
Number of ways dp[i] = dp[i-1] + dp[i-2] + dp[i-3] to include all possibilities.Final Answer:
Use dynamic programming with dp[i] = dp[i-1] + dp[i-2] + dp[i-3] -> Option AQuick Check:
Include all step sizes in sum for dp [OK]
- Using old formula for only 1 or 2 steps
- Ignoring 3-step moves
- Multiplying counts instead of adding
Ready to prove it?
Pass the test and this pattern moves unknown
