Understanding Climbing Stairs
Count the distinct ways to climb n stairs when each move climbs either one or two stairs. Taking no moves is the single way to climb zero stairs.
How it works
Every route ends with either a one-step move or a two-step move, and these cases do not overlap.
Set dp[0] = dp[1] = 1 and compute dp[i] = dp[i − 1] + dp[i − 2].
Try an example
Input: n = 5
Output: 8
Ways to reach stair 5 are the ways to reach stair 4 plus the ways to reach stair 3.
Constraints
- 0 ≤ n ≤ 30.
- Order matters: 1 + 2 and 2 + 1 are different ways.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.