Climbing Stairs — LeetCode 70 Python Solution
EasyMemoizationMathDynamic Programming
- Problem
- #70
- Pattern
- Dynamic Programming
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are climbing a staircase. It takes n steps to reach the top.
Example
- Input
- n = 2
- Output
- 2
- Explanation
- There are two ways to climb to the top.
Python solution
Python
class Solution:
def climbStairs(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return bComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) auxiliary |
Pattern: Dynamic Programming
Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 70. Climbing Stairs is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming and Memoization.
The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.
Related problems
On study lists
This problem is on Blind 75, NeetCode 150, Grind 75 and Top Interview 150.
Frequently asked questions
- How hard is LeetCode 70. Climbing Stairs?
- LeetCode 70. Climbing Stairs is rated Easy on LeetCode.
- What is the time complexity of LeetCode 70. Climbing Stairs?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 70. Climbing Stairs?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 70. Climbing Stairs cover?
- LeetCode 70. Climbing Stairs is tagged Memoization, Math and Dynamic Programming on LeetCode.