Notice Period

Ridge Runoff to Both Seas

medium

A mountainous island is mapped as a grid of elevations. The north and west coasts border Sea A; the south and east coasts border Sea B. Rain on a cell flows to an orthogonal neighbor only if that neighbor's elevation is less than or equal to the current cell's, and it reaches a sea by flowing off the matching edge of the grid.

Given the grid heights, return the coordinates [r, c] of every cell whose runoff can reach both seas. Order doesn't matter.

Example 1
in heights = [[1,2,2],[3,5,1],[2,1,2]]
out [[0,1],[0,2],[1,0],[1,1],[2,0]]
The peak at 5 drains north/west to Sea A and south/east to Sea B; the listed ridge cells manage both as well.
Example 2
in heights = [[4]]
out [[0,0]]
A single cell touches all four edges.
Constraints
  • · 1 <= rows, cols <= 12
  • · 0 <= heights[r][c] <= 10^5
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.