Loading problems...
TL;DR: The number of distinct ways to reach step n is the sum of the ways to reach step n-1 and step n-2, following the Fibonacci sequence logic.
The Climbing Stairs problem asks us to determine the total number of distinct ways to reach the top of a staircase with n steps. At any given point, you are allowed to climb either 1 or 2 steps. This is a fundamental problem in algorithmic interviews, serving as the standard introduction to dynamic programming. The LeetCode 70 solution requires identifying that the problem can be broken down into smaller, overlapping subproblems.
A naive approach attempts to simulate every possible combination of steps using recursion. To define the number of ways to reach step n, denoted as climb(n), we observe that the final move must have been either a 1-step jump from n-1 or a 2-step jump from n-2.
We can express this mathematically as:
climb(n) = climb(n-1) + climb(n-2)
Pseudo-code:
textfunction climb(n): if n == 1 return 1 if n == 2 return 2 return climb(n-1) + climb(n-2)
Time Complexity:
Analysis: This approach exhibits exponential time complexity because the recursion tree grows exponentially with n.
Why it fails:
The brute force method fails due to Time Limit Exceeded (TLE) on larger inputs (e.g., ). The algorithm redundantly recalculates the same subproblems many times. For instance, to calculate climb(5), it calculates climb(3) twice, climb(2) three times, and so on. This overlapping subproblem property indicates that pure recursion is inefficient.
The critical intuition for the LeetCode 70 solution lies in the "optimal substructure" property. To arrive at step i, the immediate previous position must have been step i-1 (taking a single step) or step i-2 (taking a double step). There is no other way to reach step i.
Consequently, the total distinct ways to reach step i is the sum of the ways to reach step i-1 and the ways to reach step i-2. This establishes the recurrence relation:
By computing these values iteratively from the bottom up (starting at step 1 and 2), we enforce the constraint that every future step is built upon valid previous configurations.
Visual Description:
Visualize the solution as a linear array or a dependency graph. The value at index i is a sink node collecting flow from nodes i-1 and i-2. The computation moves strictly from left to right. We do not need to look back further than two indices, meaning the "window" of relevant state is constant size (2 elements) as it slides toward n.

We implement the solution using an iterative bottom-up approach to avoid recursion depth issues and redundant calculations.
dp[i] represent the number of distinct ways to reach step i.dp[i] = dp[i-1] + dp[i-2].dp[i] only relies on the immediate two predecessors, we do not need to maintain an array of size n. We can use two variables to store the values of dp[i-1] and dp[i-2], updating them as we iterate. This reduces the space complexity from linear to constant.n is 1, return 1.prev2 = 1 (representing ways to reach step 1).prev1 = 2 (representing ways to reach step 2).i = 3 to n.
current = prev1 + prev2.prev2 to hold the value of prev1.prev1 to hold the value of current.prev1 contains the number of ways to reach step n.The algorithm is correct by mathematical induction.
cppclass Solution { public: int climbStairs(int n) { // Base cases if (n <= 2) { return n; } // prev2 represents ways to reach (i-2) // prev1 represents ways to reach (i-1) int prev2 = 1; int prev1 = 2; for (int i = 3; i <= n; ++i) { int current = prev1 + prev2; prev2 = prev1; prev1 = current; } return prev1; } };
javaclass Solution { public int climbStairs(int n) { // Base cases if (n <= 2) { return n; } // prev2 tracks step (i-2), prev1 tracks step (i-1) int prev2 = 1; int prev1 = 2; for (int i = 3; i <= n; i++) { int current = prev1 + prev2; prev2 = prev1; prev1 = current; } return prev1; } }
pythonclass Solution: def climbStairs(self, n: int) -> int: # Base cases if n <= 2: return n # prev2 represents step (i-2) # prev1 represents step (i-1) prev2 = 1 prev1 = 2 for i in range(3, n + 1): current = prev1 + prev2 prev2 = prev1 prev1 = current return prev1
n exactly once. Each iteration involves constant-time arithmetic operations.prev1 and prev2) to store the necessary state history, regardless of the input size n.The DP - 1D Array (Fibonacci Style) pattern used in Climbing Stairs is directly applicable to several other popular interview questions. Understanding this core logic allows you to solve:
Q: Why does the naive recursive solution result in TLE?
climb(n) = climb(n-1) + climb(n-2) without memoization.Q: Why does my loop start from 3 instead of 0 or 1?
Q: Is it wrong to use an array dp[n+1]?