Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Easy Dynamic Programming High frequency Solution Available Practice Mode

Climbing Stairs LeetCode Solution

Tags: #DP #Math #Memoization

Approach Summary

dp[i] = dp[i-1] + dp[i-2]. Equivalent to Fibonacci. Space-optimized: track only last two values.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Dynamic Programming Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Dynamic Programming pattern.

Framework: Dynamic Programming • Interview Prep: Tested at FAANG • Explore all Dynamic Programming problems & invariants →

How to Recognize This Pattern

  • Count ways to reach nth step
  • Fibonacci-like recurrence

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

DP Math Memoization

Frequently Asked Questions

What is the optimal time and space complexity for Climbing Stairs?
The optimal solution for Climbing Stairs achieves O(n) runtime complexity and requires O(1) auxiliary space. Time complexity is O(n) because we perform a single linear loop from 2 to n. Space complexity is O(1) since we only keep track of two previous step counts in scalar variables.
How do I identify that Climbing Stairs requires the Dynamic Programming pattern?
In technical interviews, look for key problem signals: Count ways to reach nth step; Fibonacci-like recurrence. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Dynamic Programming strategy.
What critical edge cases must be handled in Climbing Stairs?
Essential edge cases to verify include: n = 1 (only 1 way: take 1 step; returns 1 immediately); n = 2 (2 ways: [1, 1] or [2]; returns 2); Large values up to n = 45 (computes under 1ms without overflow in 32-bit signed integer limits). Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Climbing Stairs?
Climbing Stairs is a Easy-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Adobe, Apple.
Buy me a coffee
Daily Practice
0/0