Skip to content
AI360Xpert
Beta
Difficulty: Easy1-D Dynamic Programming

Climbing Stairs

Problem in Plain English

Count how many distinct ways there are to reach step n when every move climbs either 1 or 2 steps.

Problem Statement

You are climbing a staircase with n steps. Each move climbs either 1 step or 2 steps.

Return the number of distinct move sequences that land exactly on step n.

Constraints

  • 1 <= n <= 45
  • The answer fits in a 32-bit signed integer.

Examples

Example 1

Input
n = 5
Output
8

The routes are [1, 1, 1, 1, 1], [2, 1, 1, 1], [1, 2, 1, 1], [1, 1, 2, 1], [1, 1, 1, 2], [2, 2, 1], [2, 1, 2], and [1, 2, 2]. Order matters: placing a two-step move at different positions creates different routes.

Example 2

Input
n = 3
Output
3

The three routes are [1, 1, 1], [1, 2], and [2, 1]. The last two contain the same moves in different orders and must both be counted.

Example 3

Input
n = 1
Output
1

Only [1] reaches the first step. A two-step move overshoots, so the smallest valid input has exactly one route.

Example 4

Input
n = 2
Output
2

The two routes are [1, 1] and [2]. This second seed is needed before the recurrence can combine two smaller positive staircases.

Intuition

Group routes by their final move. A route to step 5 ends with one step from step 4 or two steps from step 3. There are 5 routes to step 4 and 3 to step 3, so step 5 has 8 routes. These groups cannot overlap because their final moves differ. Repeating this reasoning gives a recurrence; remembering the last two counts avoids counting the same smaller staircase again and again.

Approaches

Recursion Without Memo

Solution Details

Reveal Recursion Without Memo: intuition, complexity, and code

Hints

Hint 1
Ask where the final move comes from: step n is reached from step n - 1 with a single or from step n - 2 with a double.
Hint 2
The final-move groups give W(n)=W(n−1)+W(n−2)W(n) = W(n - 1) + W(n - 2) with seeds W(1)=1W(1) = 1 and W(2)=2W(2) = 2.
Hint 3
Only the last two counts matter, so keep two variables and roll them forward instead of building a table.

Edge Cases

  • n = 1 returns 1 from the seed with no loop or recursive branching.
  • n = 2 has two routes and returns directly in both snippets; n = 3 is the first input that combines two earlier counts.
  • n = 45 returns 1836311903. This fits a signed 32-bit integer, but reaching it with uncached recursion is impractical.
  • The statement excludes n = 0. In a recurrence using zero as a seed, the empty route counts once; that convention is not an additional valid input here.

Common Mistakes and Interview Tips

  • Using Fibonacci seeds 0 and 1 directly gives the wrong offset: staircase counts begin 1, 2, 3, 5, 8 for positive n.
  • Overwriting prev2 before calculating current loses one old count. Sum first, then shift both variables.
  • Stopping the loop before i = n returns the count for the previous step.
  • Treating [1, 2] and [2, 1] as the same route undercounts: move order matters.
  • Allocating a full n-sized table when the recurrence only reads the previous two entries wastes linear space.
  • Recomputing shared steps with plain recursion explodes to exponential time on larger n.

Key Takeaway

When each state depends only on a fixed window of previous states, a recurrence plus a few rolling variables turns exponential recursion into linear time and constant space.