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)

Guess you like

Origin www.cnblogs.com/sushome/p/12565756.html