70. Climbing Stairs
Climbing Stairs (LeetCode 70) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Fibonacci Style) subpattern.
- Blind 75 · #49 of 75 · 1-D Dynamic Programming
- NeetCode 150 · #99 of 150 · 1-D Dynamic Programming
- NeetCode 250 · #174 of 250 · 1-D Dynamic Programming
How to solve Climbing Stairs
Climbing Stairs is a Fibonacci Style DP problem, part of Dynamic Programming (DP) Patterns. Solve problems with Fibonacci-like recurrence: climbing stairs, house robber, decode ways.
Work towards the solution in these steps:
- To reach nth step, what could have been your previous steps? (Think about the step sizes)
The reference solution runs in O(logn) time and O(1) space.
The Climbing Stairs solution and editorial walks through the full approach with reference code.
Problem statement
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?
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
Topics and companies
Topics: Math, Dynamic Programming, Memoization.
Reported in interviews at Adobe, Amazon, Apple, Bloomberg, Expedia, Facebook, Goldman Sachs, Google, Intuit, Microsoft, Nvidia.
How to practise Climbing Stairs on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(logn) time and O(1) space.
Open Climbing Stairs in the code editor, or read the Climbing Stairs editorial for a worked solution with its approach and complexity analysis.
Where Climbing Stairs sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- 1D Array (Fibonacci Style) subpattern
- The full DSA patterns sheet
Problems related to Climbing Stairs
Other problems that use the same 1D Array (Fibonacci Style) and Dynamic Programming (DP) Patterns techniques:
- Decode Ways — Medium, same subpattern
- Delete and Earn — Medium, same subpattern
- Fibonacci Number — Easy, same subpattern
- House Robber — Medium, same subpattern
- House Robber II — Medium, same subpattern
- Integer Break — Medium, same subpattern
Climbing Stairs FAQ
What is the LeetCode number of Climbing Stairs?
Climbing Stairs is LeetCode problem 70, rated Easy.
What pattern is used to solve Climbing Stairs?
Climbing Stairs is a Fibonacci Style DP problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Fibonacci Style) subpattern.
What is the time complexity of Climbing Stairs?
The reference solution runs in O(logn) time and O(1) space.
Which companies ask Climbing Stairs?
Climbing Stairs has been reported in interviews at Adobe, Amazon, Apple, Bloomberg, Expedia, Facebook, Goldman Sachs, Google, Intuit, Microsoft, Nvidia.