MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Array/MinimumPathSum.py:31–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29
30"""
31class Solution(object):
32 def get_up_left(self, x, y):
33 if y-1 < 0:
34 up = False
35 else:
36 up = (x, y-1)
37 if x-1 < 0:
38 left = False
39 else:
40 left = (x-1,y)
41
42 # up and left
43 return (up, left)
44
45 def minPathSum(self, grid):
46 """
47 :type grid: List[List[int]]
48 :rtype: int
49 """
50
51 for i in range(len(grid)):
52 for j in range(len(grid[0])):
53 xy = self.get_up_left(j, i)
54 up = grid[xy[0][1]][xy[0][0]] if xy[0] else float('inf')
55 left = grid[xy[1][1]][xy[1][0]] if xy[1] else float('inf')
56
57 if up == float('inf') and left == float('inf'):
58 continue
59 grid[i][j] = grid[i][j] + min(up, left)
60
61
62 return grid[-1][-1]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected