Climbing Stairs
Implement climbStairs
Given a staircase of n steps, and the freedom to climb either 1 or 2 steps at a time, find how many distinct ways there are to reach the top.
Every way of reaching a given step ends in exactly one of two moves: the last move was a single step from the step just below, or a double step from two steps below. That means the number of ways to reach any step is simply the sum of the ways to reach the two steps before it — the same idea behind the Fibonacci sequence, just counting climbing paths instead of numbers. Starting from the two easy base cases (there's exactly 1 way to be standing on step 1, and 2 ways to be standing on step 2) and sweeping upward, each step's count can be built from the two counts that came immediately before it.
Example 1:
Input: n = 4
Output: 5
Example 2:
Input: n = 6
Output: 13
Example 3:
Input: n = 5
Output: 8
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ n ≤ 30
n =
4