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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find all the different ways to climb the stairs, we'll try every possible combination of taking one step or two steps at a time. We will explore each path until we reach the top and count valid ways.
Here's how the algorithm would work step-by-step:
def climbing_stairs_brute_force(number_of_stairs):
number_of_ways = 0
def climb(current_stair):
nonlocal number_of_ways
# We reached the top, increment count
if current_stair == number_of_stairs:
number_of_ways += 1
# If we went past the top, this path is invalid
elif current_stair > number_of_stairs:
return
# Explore taking one step
climb(current_stair + 1)
# Explore taking two steps
climb(current_stair + 2)
climb(0)
return number_of_ways
The optimal way to solve the climbing stairs problem is to realize you can build up the answer from smaller subproblems. Instead of trying every possible path, you figure out how many ways you can reach each step, and then use that to calculate the total number of ways to reach the top.
Here's how the algorithm would work step-by-step:
def climbing_stairs(number_of_steps):
if number_of_steps <= 2:
return number_of_steps
first_step_ways = 1
second_step_ways = 2
# Start from the third step and go to the top.
for i in range(3, number_of_steps + 1):
# Ways to reach current step is sum of ways to reach the previous two.
current_step_ways = first_step_ways + second_step_ways
first_step_ways = second_step_ways
# Store the current step's ways for the next calculation.
second_step_ways = current_step_ways
return second_step_ways
Case | How to Handle |
---|---|
n = 0 (No stairs) | Return 1, as there's one way to climb zero stairs (do nothing). |
n = 1 (One stair) | Return 1, as there's only one way to climb one stair (take one step). |
n = 2 (Two stairs) | Return 2, as there are two ways to climb two stairs (two single steps or one double step). |
Large n (Potential for integer overflow) | Use a 64-bit integer type (long in Java/C++) or Python's arbitrary-precision integers to avoid overflow. |
Negative n (Invalid input) | Throw an IllegalArgumentException or return an error code indicating invalid input. |
Recursive solution exceeding stack limit for very large n | Use dynamic programming (iterative approach) to avoid stack overflow. |
Non-integer input for n | Throw an IllegalArgumentException since only integers are valid for the number of stairs. |
n equal to the maximum possible integer value | Verify the language used supports incrementing the prior two calculated values without overflow, and handle appropriately. |