EasyDp
Climbing Stairs
amazongoogleappleadobetiktok
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?
Examples
Example 1
Input
n = 2
Output
2
Two ways: (1+1) or (2).
Example 2
Input
n = 5
Output
8
Eight ways: 1+1+1+1+1, 1+1+1+2, 1+1+2+1, 1+2+1+1, 2+1+1+1, 1+2+2, 2+1+2, 2+2+1.
Constraints
- 1 <= n <= 45
Optimal complexity
Time
O(n)
Space
O(1)
One problem, two ways to prep
Choose between solo practice and interview simulation
Practice Mode keeps things simple with code + tests. AI Interview Mode adds voice, pressure, and a post-round score summary.