Practice · Dynamic programming (1D) · 1 of 8

Count Ways Up a Staircase

A staircase has n steps. Each move takes you up either one step or two steps.

Return how many different sequences of moves take you from the bottom to exactly the top step. Sequences that differ at any move count separately, so 1 + 2 and 2 + 1 are two ways.

Example 1

Input
n = 3
Output
3
1 + 1 + 1, 1 + 2 and 2 + 1.

Example 2

Input
n = 4
Output
5
1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1 and 2 + 2.

Constraints

  • 1 ≤ n ≤ 45