Climbing Stairs Easy 0 attempts
LeetCode ↗

Climbing Stairs

Easy Dynamic ProgrammingMemoization LeetCode

You are climbing a staircase with n steps. Each time you can climb 1 or 2 steps. How many distinct ways can you reach the top?

Example: n = 3 → Output: 3 (1+1+1, 1+2, 2+1)

Sample Input
Sample Output
Constraints
  • 1 <= n <= 45
Test Cases
Case 1
Args: [2] Expected: 2
Case 2
Args: [3] Expected: 3
Case 3
Args: [5] Expected: 8

Dynamic Programming (Fibonacci)

function climbStairs(n) {
  if (n <= 2) return n;
  let a = 1, b = 2;
  for (let i = 3; i <= n; i++) [a, b] = [b, a + b];
  return b;
}

Time: O(n) | Space: O(1)

Saved in this browser only. Private to you.

JavaScript