Imagine a mysterious rectangular island surrounded by two different oceans! ๐๏ธ The Pacific Ocean laps against the island's left and top edges, while the Atlantic Ocean touches the right and bottom edges.
The island is divided into an m ร n grid where each cell has a specific height above sea level. When it rains, water flows from higher cells to neighboring cells (north, south, east, west) that are at the same or lower height.
Your Mission: Find all cells where rainwater can flow to BOTH the Pacific and Atlantic oceans! Water can reach an ocean if it flows to any cell that borders that ocean.
Input: A 2D matrix heights where heights[r][c] represents the elevation at row r, column c.
Output: A list of coordinates [r, c] where water can reach both oceans.
Input & Output
Constraints
- m == heights.length
- n == heights[r].length
- 1 โค m, n โค 200
- 0 โค heights[r][c] โค 105
- Water flows from higher or equal height to lower or equal height