#70

Climbing Stairs

Easy 1D DP

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

You climb a staircase of n steps, taking 1 or 2 steps at a time. Count the distinct ways to reach the top.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: ways to climb n steps taking 1 or 2 at a time.

Why this works: your last move came from step n-1 or n-2, so ways(n) = ways(n-1) + ways(n-2).

Example: n = 4

ways: 1, 1, 2, 3, 5 → 5

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Plain recursion without memoisation — exponential time.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon