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?
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?
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.
Sign in to submit, save progress and mark problems as solved. You can still run your code.
Loading editor...
n =
2
Expected
2