Pacific Atlantic Water Flow
A rectangular island is described by an m x n grid heights, where heights[r][c] is the height of the cell in row r and column c. The Pacific Ocean touches the island's top and left edges, and the Atlantic Ocean touches its bottom and right edges.
Rain on a cell can flow to a neighbouring cell (up, down, left or right) when that neighbour's height is less than or equal to the current cell's height. From any cell on an edge, water can flow straight into the ocean on that edge.
Return the coordinates [r, c] of every cell from which rain water can reach both oceans. The pairs may be returned in any order.
Constraints
1 <= m, n <= 2000 <= heights[r][c] <= 10^5
Examples
Example 1
Input: heights = [[1,3,2,4],[2,4,3,3],[3,2,5,1],[4,1,2,2]] Output: [[0,3],[1,1],[1,2],[1,3],[2,0],[2,2],[3,0]]
Rain on (2,2), height 5, can run to (1,2) (height 3) and on to (0,2) (height 2) on the top edge: the Pacific. It can also run straight to (3,2) on the bottom edge: the Atlantic. (0,0) borders only the Pacific, and all its neighbours are higher, so it is not in the answer.
Example 2
Input: heights = [[7]] Output: [[0,0]]
A single cell touches all four edges, so it borders both oceans.
Example 3
Input: heights = [[3,2,1]] Output: [[0,0],[0,1],[0,2]]
In a single row every cell touches the top edge (Pacific) and the bottom edge (Atlantic), so every cell counts.
JavaScript you'll use: Graphs and grids in JavaScript Arrays: create, read, update
/**
* @param {number[][]} heights
* @return {number[][]}
*/
function pacificAtlantic(heights) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.