Skip to main content

Climbing Stairs

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Problems36 / 152
Sign in

Climbing Stairs

EASYMathDynamic Programming

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1

Input:  n = 2
Output: 2

Example 2

Input:  n = 3
Output: 3
Show hint

Use dynamic programming: ways[i] = ways[i-1] + ways[i-2]. This is essentially Fibonacci sequence. Can optimize to O(1) space.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
n =
2
Expected
2
Run with your own input (JSON)