0
0
DSA Typescriptprogramming

Climbing Stairs Problem in DSA Typescript

Choose your learning style9 modes available
Mental Model
You want to find how many ways you can reach the top of stairs by taking 1 or 2 steps at a time.
Analogy: Imagine climbing stairs where you can take either one step or jump two steps at once. Counting all possible ways to reach the top is like counting all different paths you can take.
Start -> [Step 0]
Step 1 -> [Step 1]
Step 2 -> [Step 2]
...
Step n -> [Top]
Dry Run Walkthrough
Input: stairs: 4 steps
Goal: Find how many distinct ways to reach step 4 by taking 1 or 2 steps at a time
Step 1: At step 0, ways to reach = 1 (starting point)
ways[0] = 1
Why: We start at the bottom, so there is exactly one way to be there
Step 2: Calculate ways to reach step 1: ways[1] = ways[0] = 1
ways = [1, 1]
Why: From step 0, only one way to step 1 by taking one step
Step 3: Calculate ways to reach step 2: ways[2] = ways[1] + ways[0] = 1 + 1 = 2
ways = [1, 1, 2]
Why: Step 2 can be reached from step 1 or step 0
Step 4: Calculate ways to reach step 3: ways[3] = ways[2] + ways[1] = 2 + 1 = 3
ways = [1, 1, 2, 3]
Why: Step 3 can be reached from step 2 or step 1
Step 5: Calculate ways to reach step 4: ways[4] = ways[3] + ways[2] = 3 + 2 = 5
ways = [1, 1, 2, 3, 5]
Why: Step 4 can be reached from step 3 or step 2
Result:
ways = [1, 1, 2, 3, 5]
Answer: 5 ways to climb 4 steps
Annotated Code
DSA Typescript
class ClimbingStairs {
  static climbStairs(n: number): number {
    if (n <= 1) return 1
    const ways: number[] = new Array(n + 1).fill(0)
    ways[0] = 1 // base case: 1 way to stand at step 0
    ways[1] = 1 // base case: 1 way to reach step 1
    for (let i = 2; i <= n; i++) {
      ways[i] = ways[i - 1] + ways[i - 2] // sum ways from previous two steps
    }
    return ways[n]
  }
}

// Driver code
const stairs = 4
const result = ClimbingStairs.climbStairs(stairs)
console.log(result)
if (n <= 1) return 1
handle base cases where stairs are 0 or 1
ways[0] = 1 // base case: 1 way to stand at step 0
initialize ways to stand at the bottom
ways[1] = 1 // base case: 1 way to reach step 1
initialize ways to reach first step
for (let i = 2; i <= n; i++) { ways[i] = ways[i - 1] + ways[i - 2] }
calculate ways to reach step i by summing ways to reach previous two steps
return ways[n]
return total ways to reach top step n
OutputSuccess
5
Complexity Analysis
Time: O(n) because we calculate ways for each step from 2 to n once
Space: O(n) because we store ways for all steps from 0 to n
vs Alternative: Compared to recursive approach without memoization which is exponential, this dynamic programming approach is efficient and avoids repeated calculations
Edge Cases
n = 0 (no stairs)
Returns 1 because standing at the bottom counts as one way
DSA Typescript
if (n <= 1) return 1
n = 1 (one stair)
Returns 1 because only one step to climb
DSA Typescript
if (n <= 1) return 1
When to Use This Pattern
When you see a problem asking for number of ways to reach a point with fixed step sizes, use dynamic programming to build solutions from smaller steps.
Common Mistakes
Mistake: Not handling base cases n=0 or n=1 correctly, causing wrong answers or errors
Fix: Add explicit checks for n <= 1 returning 1
Mistake: Using recursion without memoization leading to slow exponential time
Fix: Use bottom-up dynamic programming with an array to store intermediate results
Summary
Calculates how many ways to climb n stairs taking 1 or 2 steps at a time.
Use when counting distinct paths with fixed step increments.
The number of ways to reach step n equals sum of ways to reach steps n-1 and n-2.