26 · Graphs: BFS & DFS

#417 Pacific Atlantic Water Flow

Mediumsolve on LeetCode ↗

frequently reported atAmazonGoogle

the problem

In plain words

heights is an island. The Pacific touches the top and left edges; the Atlantic the bottom and right. Water flows to a neighbour with height ≤ the current one. Return every cell from which water can reach both oceans.

in heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]

out [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]

in heights = [[1]]

out [[0,0]]

  • 1 ≤ m, n ≤ 200
  • 0 ≤ heights[r][c] ≤ 10⁵

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #130
      Surrounded Regions
      Mediumin Graphs
    2. #200
      Number of Islands
      Mediumin Graphs
    3. #329