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 <= 45When 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:
The brute force approach to climbing stairs is like exploring every single path you could possibly take to reach the top. It involves systematically checking out each sequence of steps you might make.
Here's how the algorithm would work step-by-step:
def climb_stairs_brute_force(number_of_stairs): def explore_paths(current_stair): # Base case: if we've reached the top, we found one valid way. if current_stair == number_of_stairs: return 1 # Base case: if we've gone past the top, this path is invalid. if current_stair > number_of_stairs: return 0 # Recursively explore taking one step and taking two steps. ways_from_one_step = explore_paths(current_stair + 1) ways_from_two_steps = explore_paths(current_stair + 2) # Summing up the ways from both possible next moves. return ways_from_one_step + ways_from_two_steps # Start the exploration from the bottom (stair 0). return explore_paths(0)To figure out how many distinct ways there are to reach the top, we can realize that the number of ways to reach a certain step depends only on the ways to reach the steps just before it. This pattern allows us to build up the solution from the bottom.
Here's how the algorithm would work step-by-step:
def climb_stairs(total_steps_to_climb):
if total_steps_to_climb == 1:
return 1
# Initialize base cases for the first two steps.
ways_to_reach_previous_step = 1
ways_to_reach_two_steps_before = 1
# Iterate from the third step up to the total number of steps.
for current_step_number in range(2, total_steps_to_climb + 1):
# The number of ways to reach the current step is the sum of ways to reach the two preceding steps.
current_step_ways = ways_to_reach_previous_step + ways_to_reach_two_steps_before
# Update values for the next iteration.
ways_to_reach_two_steps_before = ways_to_reach_previous_step
ways_to_reach_previous_step = current_step_ways
return ways_to_reach_previous_step| Case | How to Handle |
|---|---|
| Input n is 1 | The base case correctly returns 1, representing the single way to climb one step. |
| Input n is 2 | The base case correctly returns 2, representing the two ways (1+1 or 2) to climb two steps. |
| Input n is 3 | The algorithm should calculate 3 ways (1+1+1, 1+2, 2+1), which is consistent with the Fibonacci sequence pattern. |
| Large input n leading to integer overflow | Use a data type capable of storing larger numbers, like long long in C++ or Python's arbitrary precision integers, to prevent overflow. |
| The problem statement guarantees a positive integer n | Therefore, null or zero inputs are not expected and do not require explicit handling according to the problem constraints. |
| The problem implies a single distinct solution exists for each n | The dynamic programming approach inherently calculates a unique count for each n, ensuring no ambiguity in multiple valid solutions. |
| Recursive solution without memoization for large n | A purely recursive solution would lead to exponential time complexity and stack overflow for large n, necessitating memoization or an iterative DP approach. |
| The number of ways grows rapidly like Fibonacci sequence | The efficient iterative dynamic programming solution handles this growth effectively by calculating each step in linear time. |