...
/Solution Review: The Staircase Problem
Solution Review: The Staircase Problem
In this lesson, we will review the staircase problem from the coding challenge in last lesson.
Solution 1: Simple recursion
Press + to interact
def staircase(n, m):# base case of when there is no stairif n == 0:return 1ways = 0# iterate over number of steps, we can takefor i in range(1,m+1):# if steps remaining is smaller than the jump step, skipif i <= n:# recursive call with n i units lesser where i is the number of steps taken hereways += staircase(n-i, m)return waysprint(staircase(4,2))
Explanation
The problem is similar to the Fibonacci numbers algorithm. Instead of binary recursion, we have an m-ary recursion here. Which means every recursive makes, at most, m recursive calls.
The algorithm is pretty intuitive as well. At any step, we have the option to take steps of m
different lengths, i.e., we can either take 1
step, 2
steps, or so on up to m
steps. Each different step will result in a different path. ...