Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 931 · Grid DP

Minimum Falling Path Sum

LeetCode 931 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

dp[i][j] = matrix[i][j] + min(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]).

ಮಧ್ಯಮArrayDynamic ProgrammingMatrix
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Minimum Falling Path Sum (LeetCode 931) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Grid DP ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

A falling path enters each cell from one of the three cells directly above it (up-left, up, up-right). So the cheapest way to reach a cell is its own value plus the cheapest of those three, and the answer is the cheapest cell in the bottom row.

Approach

  1. 1

    Seed row 0 of dp with the first row of the matrix.

  2. 2

    For each later row, look at the three parents; out-of-range parents count as ∞.

  3. 3

    Fill row by row, top to bottom.

  4. 4

    Return the minimum of the last row.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Tabulation (row by row)PythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
    def minFallingPathSum(self, matrix: List[List[int]]) -> int:
        m=len(matrix)
        n=len(matrix[0])
        # @lru_cache(maxsize=None)
        # def dfs(i,j):
        #     if j<0 or j>=n:
        #         return float("inf")
        #     if i==0:
        #         return matrix[0][j]
        #     top = dfs(i-1,j)
        #     leftd = dfs(i-1,j-1)
        #     rightd = dfs(i-1,j+1)
        #     return matrix[i][j]+min(top,leftd,rightd)
        # min_sum=float("inf")
        # for j in range(n):
        #     min_sum = min(min_sum,dfs(m-1,j))
        # return min_sum

        dp=[[float("inf")]*(n) for _ in range(m)]
        for j in range(n):
            dp[0][j]=matrix[0][j]
        for i in range(1,m):
            for j in range(n):
                top=dp[i-1][j]
                leftd=float("inf")
                if j>0:
                    leftd = dp[i-1][j-1]
                rightd=float("inf")
                if j<m-1:
                    rightd = dp[i-1][j+1]
                dp[i][j] = matrix[i][j]+min(top,leftd,rightd)
        min_sum=float("inf")
        for j in range(n):
            min_sum = min(min_sum,dp[m-1][j])
        return min_sum
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Minimum Falling Path Sum — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Minimum Falling Path Sum ಹಿಂದಿನ intuition ಏನು?

A falling path enters each cell from one of the three cells directly above it (up-left, up, up-right). So the cheapest way to reach a cell is its own value plus the cheapest of those three, and the answer is the cheapest cell in the bottom row.

Minimum Falling Path Sum (LeetCode 931) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Seed row 0 of dp with the first row of the matrix. 2. For each later row, look at the three parents; out-of-range parents count as ∞. 3. Fill row by row, top to bottom. 4. Return the minimum of the last row.

Minimum Falling Path Sum ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n²). Space: O(n²) (one previous row would do). (Accepted approach: Tabulation (row by row).)

Minimum Falling Path Sum ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

The right-edge check uses m-1; it works because the matrix is square (m = n). The commented-out memo version starts from the bottom row and must try every column as an end point.