Given a matrix of heights, return all coordinates from which water can flow to both the Pacific Ocean and the Atlantic Ocean. Water can flow from a cell to another cell with equal or lower height in the four cardinal directions. Pattern focus: Flood fill. Run reverse DFS/BFS from each ocean boundary and intersect the reachable cells.
heights = 2D integer matrix
coordinates that can reach both 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:
These cells can reach both oceans.
Example 2:
Input:
heights = [[1]]
Output:
[[0,0]]
Explanation:
The single cell touches both borders.
Example 3:
Input:
heights = [[2,1],[1,2]]
Output:
[[0,0],[0,1],[1,0],[1,1]]
Explanation:
Every cell can reach both oceans in this small grid.