Skip to main content

Recursive Staircase

Definition​

The Recursive Staircase Algorithm (Fibonacci) is a dynamic programming approach to efficiently solve the problem of finding the number of ways to climb a staircase with a given number of steps, where you can take either 1 or 2 steps at a time. It optimizes the computation by storing previously calculated results, avoiding redundant calculations

Practice​

countWays(n):
if n <= 1:
return 1 # Base cases

if memo[n] is not None:
return memo[n] # Return memoized result if available

# Recursive calls
memo[n] = countWays(n-1) + countWays(n-2)

return memo[n]

# Initialize memoization array
memo = [None] * (max_steps + 1)