Minimum Falling Path Sum LeetCode solution in Python

1
class Solution:
2
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
3
for row in range(1, len(matrix)):
4
for col in range(0, len(matrix[row])):
5
if col == 0:
6
matrix[row][col] += min(
7
matrix[row - 1][col + 1], matrix[row - 1][col]
8
)
9
elif col == len(matrix[row]) - 1:
10
matrix[row][col] += min(
11
matrix[row - 1][col - 1], matrix[row - 1][col]
12
)
13
else:
14
matrix[row][col] += min(
15
matrix[row - 1][col - 1],
16
matrix[row - 1][col],
17
matrix[row - 1][col + 1],
18
)
19

20
return min(matrix[-1])

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0