Recursion: Davis' Staircase Discussions | | HackerRank

Recursion: Davis' Staircase

  • + 0 comments
    def stepPerms(n):
        memo = [-1] * (n+1)
        res = countSteps(n, memo) % 10000000007
        return res
    
    def countSteps(n, memo):
        if n == 0:
            return 1 # Stay where you are
        if n == 1:
            return 1 # There is only 1 way to reach on top by takine 1 step
        if n == 2: 
            return 2 # There are 2 ways 1+1 or 2
        if n == 3:
            return 4 # There are 4 ways 1+1+1 or 2+1 or 1+2 or 3
        if memo[n] != -1:
            return memo[n]
        memo[n] = countSteps(n-1, memo) + countSteps(n-2, memo) + countSteps(n-3, memo)
        return memo[n]