-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Path Sum.py
36 lines (27 loc) · 1.09 KB
/
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
# Link: https://leetcode.com/problems/minimum-path-sum/submissions/
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
# If grid is a NoneType, exit function
if grid is None:
return
# Get grid's dimensions
m = len(grid)
n = len(grid[0])
# Iterate first row
for i in range(1, n):
grid[0][i] += grid[0][i - 1] # <= Add current cell's and left cell's value
# Iterate first column
for j in range(1, m):
grid[j][0] += grid[j - 1][0] # <= Add current cell's and top cell's value
# Iterate inner grid
for xPos in range(1, m):
for yPos in range(1, n):
# Update cell's value
# NOTE: Only check left and top cell.
grid[xPos][yPos] += min(grid[xPos - 1][yPos], grid[xPos][yPos - 1])
# Return changed value of bottom-right cell
return grid[-1][-1]