dynamic programming-unique path

classic dynamic problem, ultimately it's iterative problem, like how to calculate the determinant of matrix. The code below is performed by @someone.

class Solution:
  def uniquePaths(self, m: int, n: int) -> int:
    if m < 1 or n < 1 or m > 100 or n > 100:
      return 0
    memo = {}
    def dp(i, j):
      if (i, j) not in memo:
        if i==n-1 and j==m-1:
          ans = 1
        else:
          if i==n-1:
            ans = dp(i, j+1)
          elif j==m-1:
            ans = dp(i+1, j)
          else:
            ans = dp(i, j+1) + dp(i+1, j)
        memo[i, j] = ans
      return memo[i, j]
   return dp(0, 0)

posted on 2020-03-25 13:41  BruceSue  阅读(110)  评论(0编辑  收藏  举报

导航