Climbing Stairs - Problem

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?

Note: Given n will be a positive integer.

Input & Output

Example 1 — Small Staircase
$ Input: n = 2
Output: 2
💡 Note: There are two ways to climb to the top: 1. 1 step + 1 step, 2. 2 steps
Example 2 — Medium Staircase
$ Input: n = 3
Output: 3
💡 Note: There are three ways: 1. 1+1+1 steps, 2. 1+2 steps, 3. 2+1 steps
Example 3 — Single Step
$ Input: n = 1
Output: 1
💡 Note: Only one way to climb 1 step: take 1 step

Constraints

  • 1 ≤ n ≤ 45

Visualization

Tap to expand
Climbing Stairs: n = 4 steps1234TOP5 Different Ways:1. 1+1+1+1 (four 1-steps)2. 1+1+2 (two 1s, one 2)3. 1+2+1 (1, then 2, then 1)4. 2+1+1 (2 first, then two 1s)5. 2+2 (two 2-steps)Pattern: f(4) = f(3) + f(2) = 3 + 2 = 5Can reach from step 2 or 3Output: 5
Understanding the Visualization
1
Input
Number of steps n to reach the top
2
Process
Count all possible combinations of 1-step and 2-step moves
3
Output
Total number of distinct ways
Key Takeaway
🎯 Key Insight: This is the Fibonacci sequence in disguise - each step count equals the sum of the previous two step counts
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 28
311.5K Views
Very High Frequency
~15 min Avg. Time
8.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen