less than 1 minute read

Problem Statement

70. Climbing Stairs

My Solution

Memoization Approach

class Solution:
    def climbStairs(self, n: int) -> int:
        memo = defaultdict()
        def dfs(steps):
            if steps < 0:
                return 0
            if steps == 0:
                return 1

            if steps in memo:
                return memo[steps]
                
            memo[steps] = dfs(steps - 1) + dfs(steps - 2)
            return memo[steps]

        return dfs(n)

I already solved and explained this question in the past. See this Journal for detailed explanation.