Problems

Climbing Stairs

easy
easy
math
dynamic-programming

A staircase has n steps. Starting below step 1, you may climb either 1 or 2 steps at a time. Return the number of distinct step sequences that reach exactly the top.

Input / output

  • Input: n: integer
  • Output: integer number of sequences

Examples

  1. n = 2 returns 2: 1+1 and 2.
  2. n = 5 returns 8.

Constraints

  • 1 <= n <= 45

Follow-up Show why this recurrence is Fibonacci-like and reduce the dynamic-programming table to two variables.

Examples

Example 1

Input: n = 2
Output: 2

Example 2

Input: n = 3
Output: 3

Example 3

Input: n = 5
Output: 8
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.