Advertisement
Guest User

MinimumPathSum

a guest
Feb 27th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. class Solution(object):
  2.     def minPathSum(self, grid):
  3.         """
  4.        :type grid: List[List[int]]
  5.        :rtype: int
  6.        """
  7.         m = len(grid)
  8.         if m == 0:
  9.             return 0
  10.         n = len(grid[0])
  11.         if n == 0:
  12.             return 0
  13.         if m == 1:
  14.             return sum(grid[0])
  15.         if n == 1:
  16.             return sum([grid[i][0] for i in range(m)])
  17.         return self.allPaths(grid, 0, 0, m, n, [])
  18.  
  19.     def allPaths(self, grid, y, x, m, n, path):
  20.         newPath = path + [grid[y][x]]
  21.         if y == m-1 and x == n-1:
  22.             return sum(newPath)
  23.         sums = []
  24.         if y < m-1:
  25.             sums.append(self.allPaths(grid, y+1, x, m, n, newPath))
  26.         if x < n-1:
  27.             sums.append(self.allPaths(grid, y, x+1, m, n, newPath))
  28.  
  29.         return min(sums)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement