Simple recursive solution using memoization


  • 0
    D
    cache = {}
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        if (m,n) in self.cache:
            return self.cache[m,n]
        if m < 1 or n < 1:
            return 0
        if m == n == 1:
            return 1
        self.cache[m,n] = self.uniquePaths(m-1,n) + self.uniquePaths(m,n-1)
        return self.cache[m,n]

Log in to reply
 

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