-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path64-minimum-path-sum.py
50 lines (44 loc) · 1.21 KB
/
64-minimum-path-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Given a m x n grid filled with non-negative numbers,
find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
"""
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid:
return 0
h, w = len(grid), len(grid[0])
x = y = 0
for y in range(h):
for x in range(w):
if x > 0 and y > 0:
if grid[y - 1][x] > grid[y][x - 1]:
grid[y][x] += grid[y][x - 1]
else:
grid[y][x] += grid[y - 1][x]
else:
if x > 0:
grid[y][x] += grid[y][x - 1]
elif y > 0:
grid[y][x] += grid[y - 1][x]
return grid[-1][-1]
if __name__ == "__main__":
grid = [
[1, 3, 1],
[1, 5, 1],
[4, 2, 1]
]
print(Solution().minPathSum(grid))