417. Pacific Atlantic Water Flow

Practice Pacific Atlantic Water Flow for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

heights is a grid of land heights. The Pacific Ocean touches the top and left edges of the grid; the Atlantic Ocean touches the bottom and right edges. Rain water moves from a cell to a neighbouring cell (up, down, left, right) when the neighbour is not higher, and flows into an ocean from any cell on that ocean's edge.

Return the coordinates [r, c] of every cell from which water can reach both oceans. The list can be in any order.

Example

heights = [[1,2,3],
           [8,9,4],
           [7,6,5]]  ->  [[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

Constraints