Skip to content

Commit aaf1d6e

Browse files
solves 64: Minimum Path Sum
1 parent 65d831b commit aaf1d6e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

python/minimum_path_sum.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# https://leetcode.com/problems/minimum-path-sum/
2+
# T: O(m*n) where n is the number of rows and m is the number of columns
3+
# S: O(1)
4+
5+
class Solution:
6+
def minPathSum(self, grid):
7+
rows, columns = len(grid), len(grid[0])
8+
9+
for column in range(columns - 2, -1, -1):
10+
grid[rows - 1][column] += grid[rows - 1][column + 1]
11+
12+
for row in range(rows - 2, -1, -1):
13+
grid[row][columns - 1] += grid[row + 1][columns - 1]
14+
15+
for row in range(rows - 2, -1, -1):
16+
for column in range(columns - 2, -1, -1):
17+
grid[row][column] += min(grid[row + 1][column], grid[row][column + 1])
18+
19+
return grid[0][0]

0 commit comments

Comments
 (0)