NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 509. Fibonacci NumberEasy
  • 7. Reverse IntegerMedium
  • 9. Palindrome NumberEasy
  • 13. Roman to IntegerEasy
  • 121. Best Time to Buy and Sell StockEasy

70. Climbing Stairs

Easy
MathDynamic ProgrammingMemoization
Answered: Mar 24, 2026
View on LeetCode

Problem Description

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?

Examples

Example 1

Input: n = 2
Output: 2
Explanation:
There are two ways to climb to the top.
  1. 1 step + 1 step
  2. 2 steps

Example 2

Input: n = 3
Output: 3
Explanation:
There are three ways to climb to the top.
  1. 1 step + 1 step + 1 step
  2. 1 step + 2 steps
  3. 2 steps + 1 step

Constraints

  • 1 <= n <= 45

💡 Hints (1)


Solutions

Complexity Analysis

Time Complexity:O(n)

We perform a single loop from 3 to n, executing a constant amount of work per iteration.

Space Complexity:O(1)

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.

Complexity Analysis

Time Complexity:O(n)

We loop from 3 to n once, doing O(1)O(1)O(1) work per step.

Space Complexity:O(1)

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.

Complexity Analysis

Time Complexity:O(n)

Each unique value from 3 to n is computed exactly once and cached, so there are O(n)O(n)O(n) total recursive calls.

Space Complexity:O(n)

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.

Complexity Analysis

Time Complexity:O(n)

The loop runs from index 3 to n, performing O(1)O(1)O(1) work at each step.

Space Complexity:O(n)

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.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Iterative with Temporary Variable
O(n)O(n)O(n)O(1)O(1)O(1)O(1)O(1)O(1) space, makes the swap logic explicit with a temp variableRequires an extra temporary variable; more verbose than necessary
Iterative - Pythonic Swap
O(n)O(n)O(n)O(1)O(1)O(1)Cleanest and most concise; optimal time and spaceSimultaneous assignment may be less intuitive in non-Python languages
Top-Down DP (Memoization)
O(n)O(n)O(n)O(n)O(n)O(n)Natural recursive structure mirrors the problem definitionO(n)O(n)O(n) extra space for memo dict and call stack; recursion overhead
Bottom-Up DP
O(n)O(n)O(n)O(n)O(n)O(n)Avoids recursion; straightforward DP table approachStores 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 O(n)O(n)O(n) time complexity and O(1)O(1)O(1) 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.


Previous49. Group Anagrams
Next121. Best Time to Buy and Sell Stock
Related posts
  • 509. Fibonacci NumberEasy
  • 7. Reverse IntegerMedium
  • 9. Palindrome NumberEasy
  • 13. Roman to IntegerEasy
  • 121. Best Time to Buy and Sell StockEasy
On this page
16 lines
1class Solution:
2 def climbStairs(self, n: int) -> int:
3 if n <= 2:
4 return n
5
6 # Initialize the first two values for the number of ways to climb 1 and 2 stairs.
7 a, b = 1, 2
8
9 # The number of ways to climb n stairs is the sum of the ways to climb (n-1) and (n-2) stairs.
10 for _ in range(3, n + 1):
11 temp = b
12 b = a + b # Update b to the next value in the sequence, which is the sum of the previous two values.
13 a = temp # Update a to the previous value of b (before the update), which is stored in temp.
14
15 # After the loop, b will contain the number of ways to climb n stairs.
16 return b
15 lines
1class Solution:
2 def climbStairs(self, n: int) -> int:
3 if n <= 2:
4 return n
5
6 # Initialize the first two values for the number of ways to climb 1 and 2 stairs.
7 a, b = 1, 2
8
9 # The number of ways to climb n stairs is the sum of the ways to climb (n-1) and (n-2) stairs.
10 for _ in range(3, n + 1):
11 # Update a and b to the next values in the sequence.
12 a, b = b, a + b
13
14 # After the loop, b will contain the number of ways to climb n stairs.
15 return b
23 lines
1class Solution:
2 def climbStairs(self, n: int) -> int:
3 if n <= 2:
4 return n
5
6 # Use a dictionary to store previously computed results for each number of stairs
7 memo = {}
8
9 def climb(n):
10 # Check if the result for n stairs is already computed and stored in the memo dictionary
11 if n in memo:
12 return memo[n]
13
14 if n <= 2:
15 return n
16
17 # Compute the number of ways to climb n stairs by summing the results for n-1 and n-2 stairs
18 memo[n] = climb(n - 1) + climb(n - 2)
19
20 return memo[n]
21
22 # Start the recursive computation with n stairs
23 return climb(n)
16 lines
1class Solution:
2 def climbStairs(self, n: int) -> int:
3 if n <= 2:
4 return n
5
6 # Create a dp array to store the number of ways to climb to each step
7 dp = [0] * (n + 1)
8 dp[1] = 1
9 dp[2] = 2
10
11 # Fill the dp array using the relation: dp[i] = dp[i - 1] + dp[i - 2]
12 for i in range(3, n + 1):
13 dp[i] = dp[i - 1] + dp[i - 2]
14
15 # The answer will be in dp[n]
16 return dp[n]