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?
1 <= n <= 45We perform a single loop from 3 to n, executing a constant amount of work per iteration.
Only two variables (a and b) plus one temporary variable are used, regardless of input size.
This approach recognizes that the number of ways to climb n stairs follows the Fibonacci recurrence: ways(n) = ways(n-1) + ways(n-2), since from step n you could have come from either n-1 or n-2. Starting from the base cases a = 1 (one way to climb 1 stair) and b = 2 (two ways to climb 2 stairs), we iteratively advance the window by saving b into a temporary variable, computing the new b as the sum of the two previous values, and shifting a up. After n - 2 iterations, b holds the answer.
We loop from 3 to n once, doing work per step.
Only two variables are maintained at any time, with no dependency on input size.
This is the same iterative approach as Solution 1, but uses simultaneous assignment (a, b = b, a + b) to advance the two pointers in a single line, eliminating the need for a temporary variable. Starting from a = 1, b = 2, each iteration shifts a to the previous b and b to their sum, so after n - 2 iterations b holds the answer.
Each unique value from 3 to n is computed exactly once and cached, so there are total recursive calls.
The memo dictionary stores up to n entries, and the recursion call stack reaches a depth of n.
This approach uses top-down dynamic programming: a recursive helper climb computes ways(n) by breaking it into ways(n-1) + ways(n-2), but stores each result in a memo dictionary before returning so that any subproblem is only solved once. On subsequent calls with the same n, the cached value is returned immediately, avoiding the exponential blowup of naive recursion.
The loop runs from index 3 to n, performing work at each step.
The dp array stores n + 1 values.
This approach uses bottom-up dynamic programming by allocating an array dp of size n + 1, seeding dp[1] = 1 and dp[2] = 2, then filling each entry from index 3 to n using the recurrence dp[i] = dp[i-1] + dp[i-2]. The answer is simply dp[n] after the loop completes.
| Approach | Rating | Time Complexity | Space Complexity | Advantages | Disadvantages |
|---|---|---|---|---|---|
| Iterative with Temporary Variable | space, makes the swap logic explicit with a temp variable | Requires an extra temporary variable; more verbose than necessary | |||
| Iterative - Pythonic Swap | Cleanest and most concise; optimal time and space | Simultaneous assignment may be less intuitive in non-Python languages | |||
| Top-Down DP (Memoization) | Natural recursive structure mirrors the problem definition | extra space for memo dict and call stack; recursion overhead | |||
| Bottom-Up DP | Avoids recursion; straightforward DP table approach | Stores all n+1 values unnecessarily when only the last two are needed |
The optimal approach for this problem is Solution 2: Iterative - Pythonic Swap, which achieves time complexity and space complexity. It delivers the same optimal performance as Solution 1 but in a single, elegant line using simultaneous assignment, making it the most concise and idiomatic implementation.
class Solution: def climbStairs(self, n: int) -> int: if n <= 2: return n
# Initialize the first two values for the number of ways to climb 1 and 2 stairs. a, b = 1, 2
# The number of ways to climb n stairs is the sum of the ways to climb (n-1) and (n-2) stairs. for _ in range(3, n + 1): temp = b b = a + b # Update b to the next value in the sequence, which is the sum of the previous two values. a = temp # Update a to the previous value of b (before the update), which is stored in temp.
# After the loop, b will contain the number of ways to climb n stairs. return bclass Solution: def climbStairs(self, n: int) -> int: if n <= 2: return n
# Initialize the first two values for the number of ways to climb 1 and 2 stairs. a, b = 1, 2
# The number of ways to climb n stairs is the sum of the ways to climb (n-1) and (n-2) stairs. for _ in range(3, n + 1): # Update a and b to the next values in the sequence. a, b = b, a + b
# After the loop, b will contain the number of ways to climb n stairs. return bclass Solution: def climbStairs(self, n: int) -> int: if n <= 2: return n
# Create a dp array to store the number of ways to climb to each step dp = [0] * (n + 1) dp[1] = 1 dp[2] = 2
# Fill the dp array using the relation: dp[i] = dp[i - 1] + dp[i - 2] for i in range(3, n + 1): dp[i] = dp[i - 1] + dp[i - 2]
# The answer will be in dp[n] return dp[n]