easy
math
dynamic-programming
A staircase has n steps. Starting below step 1, you may climb either 1 or 2 steps at a time. Return the number of distinct step sequences that reach exactly the top.
Input / output
- Input:
n: integer - Output: integer number of sequences
Examples
n = 2returns2:1+1and2.n = 5returns8.
Constraints
1 <= n <= 45
Follow-up Show why this recurrence is Fibonacci-like and reduce the dynamic-programming table to two variables.
Examples
Example 1
Input: n = 2
Output: 2
Example 2
Input: n = 3
Output: 3
Example 3
Input: n = 5
Output: 8
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.