Skip to solution
easyDSA

How do you solve the climbing stairs problem?

1.1k views
01

Understand the problem

Explain the Fibonacci DP.

dpclimbing-stairs
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Ways to reach step n = ways(n-1) + ways(n-2) (take 1 or 2 steps), which is Fibonacci. Compute iteratively with two variables in O(n) time, O(1) space.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

O(1)-space DP
Run Playground
def climb_stairs(n):
    a, b = 1, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b


# --- demo ---
print(climb_stairs(2), climb_stairs(3), climb_stairs(5))   # 2 3 8
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 2 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.

Back to track