This problem is a classic example of dynamic programming and can be solved using either recursion with memoization or an iterative approach. The goal is to count the number of distinct ways to climb a staircase of n steps, where you can take 1 or 2 steps at a time.
Key Idea
The number of ways to reach step n depends on:
The number of ways to reach step n-1 (if you take a single step from there).
The number of ways to reach step n-2 (if you take two steps from there).
Thus, the recurrence relation is:
ways(n)=ways(n−1)+ways(n−2)
This resembles the Fibonacci sequence, where each term is the sum of the two preceding terms.
Approach 1: Recursive Solution with Memoization
To avoid redundant calculations, we use memoization to store intermediate results.
def count_ways_recursive(n, memo=None):
"""
Counts the number of ways to climb a staircase of n steps using recursion with memoization.
Parameters:
n (int): Number of steps in the staircase.
memo (dict): Dictionary to store previously computed results.
Returns:
int: Total number of ways to climb the staircase.
"""
if memo is None:
memo = {}
# Base cases
if n == 0 or n == 1:
return 1
# Check if result is already computed
if n in memo:
return memo[n]
# Recursive case
memo[n] = count_ways_recursive(n - 1, memo) + count_ways_recursive(n - 2, memo)
return memo[n]
# Example usage
print(count_ways_recursive(5)) # Output: 8
We can compute the result iteratively using a bottom-up approach, which avoids the overhead of recursion.
def count_ways_iterative(n):
"""
Counts the number of ways to climb a staircase of n steps using an iterative approach.
Parameters:
n (int): Number of steps in the staircase.
Returns:
int: Total number of ways to climb the staircase.
"""
if n == 0 or n == 1:
return 1
# Initialize base cases
prev1, prev2 = 1, 1 # Ways to reach step 0 and step 1
# Compute ways for steps 2 through n
for i in range(2, n + 1):
current = prev1 + prev2
prev2 = prev1
prev1 = current
return prev1
# Example usage
print(count_ways_iterative(5)) # Output: 8
Approach 3: Closed-Form Formula (Using Binet's Formula for Fibonacci Numbers)
Since this problem is equivalent to finding the (n+1)-th Fibonacci number, we can use the closed-form formula:
F(n)=5
where ϕ=21+5 is the golden ratio.
import math
def count_ways_closed_form(n):
"""
Counts the number of ways to climb a staircase of n steps using the closed-form Fibonacci formula.
Parameters:
n (int): Number of steps in the staircase.
Returns:
int: Total number of ways to climb the staircase.
"""
phi = (1 + math.sqrt(5)) / 2
return round((phi**(n + 1) - (1 - phi)**(n + 1)) / math.sqrt(5))
# Example usage
print(count_ways_closed_form(5)) # Output: 8
Comparison of Approaches
Approach
Time Complexity
Space Complexity
Pros
Cons
Recursive with Memoization
O(n)
O(n)
Intuitive and easy to implement
Extra space for memoization
Iterative (Dynamic Programming)
O(n)
Example Walkthrough
For n = 5, the possible ways to climb the staircase are:
[1, 1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 2, 1]
[1, 2, 1, 1]
[2, 1, 1, 1]
[1, 2, 2]
[2, 1, 2]
[2, 2, 1]
Total ways: 8
Final Answer
You can use any of the above approaches based on your requirements. For simplicity and efficiency, the iterative solution is often preferred:
def count_ways_iterative(n):
if n == 0 or n == 1:
return 1
prev1, prev2 = 1, 1
for _ in range(2, n + 1):
prev1, prev2 = prev1 + prev2, prev1
return prev1
# Example usage
print(count_ways_iterative(5)) # Output: 8
This implementation runs in O(n) time and O(1) space, making it highly efficient for large values of n.