Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 329 · Graph DP

Longest Increasing Path in a Matrix

LeetCode 329 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Strictly increasing edges form a DAG ⇒ memoised DFS.

கடினம்ArrayDynamic ProgrammingDepth-First SearchBreadth-First SearchGraph Theory
LeetCode இல் திற ↗

நான் Longest Increasing Path in a Matrix (LeetCode 329) ஐ இப்படித் தீர்த்தேன் — Graph DP பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

From any cell, you may step to a neighbour with a larger value. Because values must strictly increase there are no cycles, so the longest path from a cell is 1 + the longest path from its best larger neighbour — cache it and every cell is solved once.

Approach

  1. 1

    dfs(r, c) = longest increasing path starting at (r, c).

  2. 2

    Try the four directions; recurse only into in-bounds, strictly larger neighbours.

  3. 3

    Store the result in memo.

  4. 4

    Answer = max dfs over every cell.

நான் எப்படித் தீர்த்தேன்

DFS + memo on a DAGPythonO(m·n) time · O(m·n) space
class Solution:
    def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
        m = len(matrix)
        n = len(matrix[0])
        memo = {}
        def dfs(row,col):
            key = (row,col)
            if key in memo:
                return memo[key]
            di = [0,+1,0,-1]
            dj = [+1,0,-1,0]
            max_length = 1
            for k in range(4):
                nrow = row+di[k]
                ncol = col+dj[k]
                if 0<=nrow<m and 0<=ncol<n and matrix[nrow][ncol]>matrix[row][col]:
                       max_length=max(max_length,1+dfs(nrow,ncol))
            memo[key]=max_length
            return memo[key]
        max_length = 0
        for i in range(m):
            for j in range(n):
                max_length= max(dfs(i,j),max_length)
        return max_length
            
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Longest Increasing Path in a Matrix — அடிக்கடி கேட்கப்படும் கேள்விகள்

Longest Increasing Path in a Matrix பின்னாலுள்ள intuition என்ன?

From any cell, you may step to a neighbour with a larger value. Because values must strictly increase there are no cycles, so the longest path from a cell is 1 + the longest path from its best larger neighbour — cache it and every cell is solved once.

Longest Increasing Path in a Matrix (LeetCode 329) ஐ எப்படித் தீர்ப்பது?

1. dfs(r, c) = longest increasing path starting at (r, c). 2. Try the four directions; recurse only into in-bounds, strictly larger neighbours. 3. Store the result in memo. 4. Answer = max dfs over every cell.

Longest Increasing Path in a Matrix இன் time மற்றும் space complexity என்ன?

Time: O(m · n). Space: O(m · n). (Accepted approach: DFS + memo on a DAG.)

Longest Increasing Path in a Matrix இல் பொதுவான தவறுகள் என்ன?

No visited set is needed — strict increase already prevents cycles. Equal neighbours are not allowed.