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

Minimum Path Sum

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

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

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

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

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

Intuition

You can only arrive at a cell from the left or from above, so the cheapest route to it is its own cost plus the cheaper of those two neighbours. Build that up from the top-left corner and read the bottom-right.

Approach

  1. 1

    Top-down: dfs(i, j) walks back from the bottom-right; off-grid cells cost ∞ and (0, 0) costs grid[0][0].

  2. 2

    Bottom-up: seed dp[0][0], then fill row by row, treating missing neighbours as ∞.

  3. 3

    The answer is dp[m-1][n-1].

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Recursion + memoO(m·n) time · O(m·n) space

lru_cache dfs from the target back to the start.

2
TabulationAcceptedO(m·n) time · O(m·n) space

dp grid filled from the top-left.

Recursion + memoPythonO(m·n) time · O(m·n) space
from functools import lru_cache

class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])
        @lru_cache(maxsize=None)
        def dfs(i,j):
            if i<0 or j<0:
                return float("inf")
            if i==0 and j==0:
                return grid[0][0]
            return grid[i][j]+min(dfs(i,j-1),dfs(i-1,j))
        return dfs(m-1,n-1)
TabulationPythonO(m·n) time · O(m·n) space
class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:
        if not grid or not grid[0]:
            return 0
        m = len(grid)
        n = len(grid[0])
        dp=[[float("inf")]*n for _ in range(m)]
        dp[0][0]=grid[0][0]
        for i in range(m):
            for j in range(n):
                if i==0 and j==0:
                    continue
                step1 = dp[i][j-1] if j>0 else float("inf")
                step2 = dp[i-1][j] if i>0 else float("inf")
                dp[i][j]=grid[i][j]+min(step1,step2)
        return dp[m-1][n-1]
sol = Solution()
assert sol.minPathSum([[1,3,1],[1,5,1],[4,2,1]])==7
#write for a minus case scenario too
assert sol.minPathSum([[1000,1000,1000],[1000,1000,1000],[1000,1000,1000]])==5000
assert sol.minPathSum([[]]) == 0
assert sol.minPathSum([]) == 0
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

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

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

You can only arrive at a cell from the left or from above, so the cheapest route to it is its own cost plus the cheaper of those two neighbours. Build that up from the top-left corner and read the bottom-right.

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

1. Top-down: dfs(i, j) walks back from the bottom-right; off-grid cells cost ∞ and (0, 0) costs grid[0][0]. 2. Bottom-up: seed dp[0][0], then fill row by row, treating missing neighbours as ∞. 3. The answer is dp[m-1][n-1].

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

Time: O(m · n). Space: O(m · n). (Accepted approach: Tabulation.)

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

Skip (0, 0) inside the loop or you'll add it twice. Guard empty grids before reading grid[0].

Minimum Path Sum ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Recursion + memo (O(m·n) time · O(m·n) space): lru_cache dfs from the target back to the start. Tabulation (O(m·n) time · O(m·n) space): dp grid filled from the top-left.