Okay, let's break down how to count the number of ways to climb a staircase of n steps, taking either 1 or 2 steps at a time. This is a classic dynamic programming problem, and it's closely related to the Fibonacci sequence.
Understanding the Problem
Base Cases:
If n = 0 (no steps), there's 1 way to climb (do nothing).
If n = 1 (one step), there's 1 way to climb (take one step).
If n = 2 (two steps), there are 2 ways to climb (take two 1-steps or one 2-step).
Recursive Relationship: To reach step n, you can either:
Take a 1-step from step n-1.
Take a 2-step from step n-2.
Therefore, the number of ways to reach step n is the sum of the number of ways to reach step n-1 and the number of ways to reach step n-2.
Python Implementation (Recursive)
python
def count_ways_recursive(n):
"""
Counts the number of ways to climb n steps recursively.
Args:
n: The number of steps in the staircase.
Returns:
The number of ways to climb the staircase.
"""
if n == 0 or n == 1:
return 1
if n == 2:
return 2
return count_ways_recursive(n - 1) + count_ways_recursive(n - 2)
The recursive solution is simple, but it can be inefficient for larger values of n because it recalculates the same subproblems multiple times. Memoization (a form of dynamic programming) can significantly improve performance.
python
def count_ways_memoization(n, memo={}):
"""
Counts the number of ways to climb n steps using memoization.
Args:
n: The number of steps in the staircase.
memo: A dictionary to store previously calculated results.
Returns:
The number of ways to climb the staircase.
"""
if n in memo:
return memo[n]
if n == 0 or n == 1:
return 1
if n == 2:
return 2
memo[n] = count_ways_memoization(n - 1, memo) + count_ways_memoization(n - 2, memo)
return memo[n]
An iterative dynamic programming approach is often the most efficient.
python
def count_ways_iterative(n):
"""
Counts the number of ways to climb n steps iteratively using dynamic programming.
Args:
n: The number of steps in the staircase.
Returns:
The number of ways to climb the staircase.
"""
if n <= 1:
return 1
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1
dp[2] = 2
for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
Space Optimization (Iterative)
We can further optimize the iterative solution to use only constant space, as we only need the previous two values to calculate the current value.
python
def count_ways_space_optimized(n):
"""
Counts the number of ways to climb n steps iteratively using dynamic programming with space optimization.
Args:
n: The number of steps in the staircase.
Returns:
The number of ways to climb the staircase.
"""
if n <= 1:
return 1
a = 1 # dp[0]
b = 1 # dp[1]
for _ in range(2, n + 1):
temp = a + b
a = b
b = temp
return b
Example Usage
python
n = 5
print(f"Number of ways to climb {n} steps (recursive): {count_ways_recursive(n)}")
print(f"Number of ways to climb {n} steps (memoization): {count_ways_memoization(n)}")
print(f"Number of ways to climb {n} steps (iterative): {count_ways_iterative(n)}")
print(f"Number of ways to climb {n} steps (space optimized): {count_ways_space_optimized(n)}")
Time and Space Complexity
Recursive:
Time Complexity: O(2n) - Exponential
Space Complexity: O(n) - Due to the call stack
Memoization:
Time Complexity: O(n) - Linear
Space Complexity: O(n) - For the memoization dictionary
Iterative:
Time Complexity: O(n) - Linear
Space Complexity: O(n) - For the dp array
Space Optimized Iterative:
Time Complexity: O(n) - Linear
Space Complexity: O(1) - Constant
The iterative (especially the space-optimized) solution is the most efficient in terms of both time and space complexity. It's the preferred approach for larger values of n.