Editor

Climbing Stairs

#41 · Dynamic programming · Beginner · 40 XP · about 8 min

You are climbing a staircase with n steps. With each move you go up either 1 step or 2 steps. Write count_ways(n) that returns how many different sequences of moves take you from the ground to exactly the top step.

A staircase with 0 steps has one way to climb it: make no moves at all.

Examples

count_ways(1)  # returns 1
count_ways(3)  # returns 3
count_ways(5)  # returns 8

For 3 steps the three sequences are 1 + 1 + 1, 1 + 2 and 2 + 1.

Constraints

  • 0 <= n <= 30

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.