Pacific Atlantic Water Flow
Given an m x n grid of non-negative heights, water can flow from a cell to a neighboring cell (up/down/left/right) only if the neighbor's height is less than or equal to the current cell's height. The Pacific ocean touches the top and left edges; the Atlantic touches the bottom and right edges. Return the coordinates of every cell from which water can reach both oceans.
Open official problem prompt ↗Find every cell whose water can drain to both the Pacific and Atlantic oceans.
Rather than release a raindrop on each mountain cell and watch where it ends up, imagine the sea level rising from each coast and flooding uphill; wherever both floods overlap, a raindrop could have run down to either shore.
- Input
- 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]]
- Output
- [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
- Why
- Each listed cell has an ever-non-increasing downhill path reaching both the top/left border and the bottom/right border.
m == heights.lengthn == heights[i].length1 <= m, n <= 2000 <= heights[r][c] <= 10^5