0
0
DSA Typescriptprogramming~3 mins

Why Climbing Stairs Problem in DSA Typescript?

Choose your learning style9 modes available
The Big Idea

What if you could find all ways to climb stairs without trying every step combination?

The Scenario

Imagine you want to climb a staircase with many steps, but you can only take one or two steps at a time. You want to know how many different ways you can reach the top.

Without a smart method, you might try to list all possible step combinations by hand.

The Problem

Listing all step combinations manually is slow and confusing, especially as the number of steps grows. You might forget some combinations or repeat others, making it error-prone and frustrating.

The Solution

The Climbing Stairs Problem uses a simple counting method that builds on smaller results to find the total ways quickly. It avoids listing all combinations by using addition of previous counts, making it fast and reliable.

Before vs After
Before
function countWays(steps: number): number {
  // Manually list all combinations (impractical)
  // For example, for 3 steps: (1,1,1), (1,2), (2,1)
  return 3; // Hardcoded for small steps
}
After
function countWays(steps: number): number {
  if (steps <= 2) return steps;
  let waysOneStepBefore = 2;
  let waysTwoStepsBefore = 1;
  let totalWays = 0;
  for (let i = 3; i <= steps; i++) {
    totalWays = waysOneStepBefore + waysTwoStepsBefore;
    waysTwoStepsBefore = waysOneStepBefore;
    waysOneStepBefore = totalWays;
  }
  return totalWays;
}
What It Enables

This method lets you quickly find how many ways to climb any number of steps without listing all possibilities.

Real Life Example

Planning how many ways to reach the top of a staircase when you can hop one or two steps at a time, like in a fitness app counting exercise variations.

Key Takeaways

Manual counting is slow and error-prone for many steps.

Using previous results speeds up counting ways to climb.

This approach scales easily to large numbers of steps.