-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum_path_sum.py
34 lines (28 loc) · 1.11 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
from typing import List
class Solution:
"""
Task:
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.
"""
# Method 1
def minPathSum(self, grid: List[List[int]]) -> int:
for r in range(len(grid)):
for c in range(len(grid[0])):
if r and c:
grid[r][c] += min(grid[r-1][c], grid[r][c-1])
elif not r and c:
grid[r][c] += grid[r][c-1]
elif not c and r:
grid[r][c] += grid[r-1][c]
return grid[-1][-1]
# Method 2
def minPathSum(self, grid: List[List[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
res = [[float("inf")] * (COLS + 1) for r in range(ROWS + 1)]
res[ROWS - 1][COLS] = 0
for r in range(ROWS - 1, -1, -1):
for c in range(COLS - 1, -1, -1):
res[r][c] = grid[r][c] + min(res[r + 1][c], res[r][c + 1])
return res[0][0]