Python recursive solution with cache


  • 1
    G
    class Solution:
        # @param {integer} m
        # @param {integer} n
        # @return {integer}
        def uniquePaths(self, m, n):
            cache = {}
            return self.findPath(m, n, cache)
    
        def findPath(self, m, n, cache):
            if (m, n) in cache:
                return cache[(m, n)]
            elif m == 1 or n == 1:
                return 1
    
            cache[(m, n)] = self.findPath(m - 1, n, cache) + self.findPath(m, n - 1, cache)
    
            return cache[(m, n)]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.