Dynamic programming · Problem 1 of 3

Ways to climb a staircase

Pattern: Climbing Stairs: Intro to Dynamic Programming

easyNot solved yet

The problem

A staircase has n steps. Each move, you climb either 1 step or 2 steps. Write a function that returns how many different sequences of moves get you from the bottom to exactly the top. n is between 0 and 40. For n = 0 there's exactly one way: do nothing.

text
1climb_ways(3)  ->  3
2climb_ways(5)  ->  8
3climb_ways(0)  ->  1

For 3 steps, the ways are 1+1+1, 1+2 and 2+1.

Write climb_ways(n) in Python or climbWays(n) in JavaScript.

Runs privately in your browser. Nothing is sent to a server.Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Write your function, then run it against 12 tests.

Breakdown

Stuck? Reveal one step at a time, and try again after each one.

  1. Step 2
  2. Step 3
  3. Step 4

Solution

?

Give it a real try first: you learn far more from a wrong attempt than from reading the answer.