AlgoFrames

Climbing stairs

Standard · Write code · about 8 min

A staircase has n steps. Each move climbs 1 step or 2 steps. Return the number of different move sequences that reach the top.

Fill a table where cell i holds the number of ways to reach step i.

Example 1

Input:

[3]

Output:

3

Example 2

Input:

[5]

Output:

8

1+1+1+1+1, 1+1+1+2 in four orders, 1+2+2 in three orders.

Constraints

  • 1 ≤ n ≤ 45
  • The answer fits in a 32-bit signed integer

Learn the idea first: Tabulation: filling the table bottom-up, a visual lesson.

More Tabulation: filling the table bottom-up problems