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:
Imagine you're climbing a staircase and can take either one or two steps at a time. The brute force way is to try every single possible combination of single and double steps to reach the top. We'll count each unique path.
Here's how the algorithm would work step-by-step:
def climbing_stairs_brute_force(number_of_stairs):
def count_ways(current_step):
# If we've reached the top, it's a valid way
if current_step == number_of_stairs:
return 1
# If we've overshot, it's not a valid way
if current_step > number_of_stairs:
return 0
# Explore taking one step
one_step = count_ways(current_step + 1)
# Explore taking two steps
two_steps = count_ways(current_step + 2)
# Accumulate the total ways to reach the top
return one_step + two_steps
# Start from the bottom (step 0)
return count_ways(0)
The problem involves finding the number of ways to climb a staircase with only one or two steps at a time. Instead of exploring every single combination, we can use a more efficient method based on recognizing a pattern. This strategy builds the solution from the ground up by reusing previously computed results.
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
# Initialize base cases for 1 and 2 steps
step_one_count = 1
step_two_count = 2
# Iterate from 3 up to the total number of steps.
for i in range(3, number_of_steps + 1):
# Calculate ways to reach current step.
current_step_count = step_one_count + step_two_count
# Update variables for next iteration.
step_one_count = step_two_count
step_two_count = current_step_count
# The accumulated value holds the total number of ways.
return step_two_count
Case | How to Handle |
---|---|
n = 0 | Return 1, as there is one way to climb 0 stairs (do nothing). |
n = 1 | Return 1, as there is one way to climb 1 stair (take one step). |
n = 2 | Return 2, as there are two ways to climb 2 stairs (two single steps or one double step). |
Large n (potential integer overflow) | Use a data type capable of holding larger values, such as long or consider using a library for arbitrary-precision arithmetic if necessary. |
Negative n (invalid input) | Throw an IllegalArgumentException or return 0, indicating invalid input. |
Algorithm-specific recursion depth for very large n (if using recursion) | Implement dynamic programming or iteration to avoid stack overflow issues. |
Memoization table size in dynamic programming (if using dynamic programming) | Ensure sufficient memory is allocated for the memoization table to store intermediate results for all possible stair counts up to n. |
Input 'n' is significantly large such that generating fibonacci sequence via recursion gives StackOverflowError | Use iterative approach, dynamic programming, or memoization to avoid recursion. |