Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 608 Bytes

931.md

File metadata and controls

24 lines (22 loc) · 608 Bytes

Solution

class Solution(object):
    def minFallingPathSum(self, A):
        """
        :type A: List[List[int]]
        :rtype: int
        """
        m, n = len(A), len(A[0])
        dp = A[::]
        # for j in range(n):
        #     A[m-1][j] = A[m][j]
        
        for i in reversed(range(m - 1)):
            for j in range(n):
                tmp = A[i+1][j]
                if j > 0:
                    tmp = min(tmp, A[i+1][j-1])
                if j < n-1:
                    tmp = min(tmp, A[i+1][j+1])
                dp[i][j] += tmp
        
        return min(dp[0])