Problem Statement
There is an m x n rectangular island with heights given in a 2D matrix. The Pacific Ocean borders the left (column 0) and top (row 0) edges, while the Atlantic Ocean borders the right (column n-1) and bottom (row m-1) edges. Water flows from a cell to its adjacent cells (up, down, left, right) if the adjacent cell's height is less than or equal to the current cell's height. Return all cells from which water can flow to both oceans.[4][9]
Input/Output Format
heights where heights[i][j] is the height of cell (i, j).[[r1,c1],[r2,c2],...] representing cells that can reach both oceans (in any order).[1][9]Examples
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]]
Water can flow from these 7 cells to both oceans.[4]
Example 2:
Input: heights = [[2,2],[2,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]
All cells reach both oceans since heights are equal.[4]
Constraints
m == heights.lengthn == heights[i].length1 <= m, n <= 2000 <= heights[r][c] <= 10^5[3][1]