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

मी 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.