Water flowing to both oceans
Mediumgraphdfsbfsmatrix
heights is an elevation map. One ocean touches the top and left edges, the other the bottom and right edges. Water flows to a neighbouring cell (up/down/left/right) whose height is lower or equal. Return every [row, col] whose water can reach both oceans, sorted by row then column.
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]]
Example 2
Input: heights = [[1]] Output: [[0,0]]
Constraints
- 1 ≤ rows, cols ≤ 100
- 0 ≤ heights[r][c] ≤ 10⁵
Submit checks your solution against 7 tests, including hidden ones.
/**
* @param {int[][]} heights
* @return {int[][]}
*/
function pacificAtlantic(heights) {
}