There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c). The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean. Return a 2D list of grid coordinates result where result[i] = [r_i, c_i] denotes that rain water can flow from cell (r_i, c_i) to both the Pacific and Atlantic oceans.
Example 1
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]]
Explanation: Water flows to a neighbour of equal or lower height. Each listed cell has a downhill path to the top or left edge (Pacific) and another to the bottom or right edge (Atlantic). For example, the 5 at (2,2) can flow up through 3 and 2 to the top edge, and right through 3 and 1 to the right edge.
Example 2
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The single cell touches all four edges, so it drains into both oceans.
Example 3
Input: heights = [[2,1],[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]
Explanation: Every cell touches at least one ocean directly, and each 2 can flow down to a neighbouring 1 on the opposite side. For example, (0,0) touches the Pacific and flows to (0,1), which touches the Atlantic. So all four cells reach both oceans.
Instead of asking whether each cell can reach an ocean, flip it: start at the ocean borders and climb inward to cells of equal or greater height. Cells reached from both oceans are the answer.
Run checks all cases above. Submit evaluates all test cases.