Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 417 · Graph Fundamentals / DFS-BFS

Pacific Atlantic Water Flow

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

Reverse BFS from each ocean; intersect.

ಮಧ್ಯಮArrayDepth-First SearchBreadth-First SearchMatrix
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Pacific Atlantic Water Flow (LeetCode 417) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Graph Fundamentals / DFS-BFS ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

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

Intuition

Instead of testing every cell, flow water uphill from each ocean. BFS from the Pacific border into cells that are at least as high, then from the Atlantic border; cells reachable from both can drain to both oceans.

Approach

  1. 1

    Pacific starts: top row and left column. Atlantic: bottom row and right column.

  2. 2

    BFS into neighbours with height ≥ current.

  3. 3

    Return cells in both reachable sets.

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

Two reverse BFSPythonO(m·n) time
class Solution:
    def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
        m = len(heights)
        n = len(heights[0])
        di = [0,+1,0,-1] #top,right,bottom,left
        dj = [+1,0,-1,0] #top,right,bottom,left
        pacific_q=deque([])
        atlantic_q = deque([])
        for i in range(m):
            pacific_q.append((i,0))
            atlantic_q.append((i,n-1))
        for j in range(n):
            pacific_q.append((0,j))
            atlantic_q.append((m-1,j))
        def bfs(q):
            reachable = set()
            while q:
                srow,scol = q.popleft()
                reachable.add((srow,scol))
                for k in range(4):
                    nrow = srow+di[k]
                    ncol = scol+dj[k]
                    if 0<=nrow<m and 0<=ncol<n and heights[nrow][ncol]>=heights[srow][scol]:
                        if (nrow,ncol) not in reachable:
                            reachable.add((nrow,ncol))
                            q.append((nrow,ncol))
            return reachable
        pacific_reachable = bfs(pacific_q)
        atlantic_reachable = bfs(atlantic_q)
        return list(pacific_reachable.intersection(atlantic_reachable))
        

        

        
                
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Pacific Atlantic Water Flow — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Pacific Atlantic Water Flow ಹಿಂದಿನ intuition ಏನು?

Instead of testing every cell, flow water uphill from each ocean. BFS from the Pacific border into cells that are at least as high, then from the Atlantic border; cells reachable from both can drain to both oceans.

Pacific Atlantic Water Flow (LeetCode 417) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Pacific starts: top row and left column. Atlantic: bottom row and right column. 2. BFS into neighbours with height ≥ current. 3. Return cells in both reachable sets.

Pacific Atlantic Water Flow ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(m · n). Space: O(m · n). (Accepted approach: Two reverse BFS.)

Pacific Atlantic Water Flow ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Water flows downhill, so the reverse search climbs uphill.