Leetcode 967 Solution

This article provides solution to leetcode question 967 (minimum-falling-path-sum)

https://leetcode.com/problems/minimum-falling-path-sum

Solution

class Solution:
    def minFallingPathSum(self, A: List[List[int]]) -> int:
        n = len(A)

        for i in reversed(range(n - 1)):
            for j1 in range(n):
                old_a = A[i][j1]
                A[i][j1] = sys.maxsize
                for j2 in [j1 - 1, j1, j1 + 1]:
                    if 0 <= j2 < n:
                        A[i][j1] = min(A[i][j1], A[i + 1][j2] + old_a)
        return min(A[0])