Loading
Loading
A hydrology model represents an island as a grid of ground heights. The Northern Sea touches the top and left edges of the grid; the Southern Sea touches the bottom and right edges. Rain falling on a cell can flow to any neighbour above, below, left or right whose height is less than or equal to the current cell's, and from any edge cell straight into the sea beside it.
Return how many cells of heights can send rainwater to both seas.
heights = [[1,2,2,3],[3,2,3,4],[2,4,5,3],[6,7,1,4]]north-reachableclimb from the top and left edgessouth-reachableclimb from the bottom and right edgesboth(0,3) (1,3) (2,2) (3,0) (3,1) — 5 cells5Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 2 | 2 | 3 |
|---|---|---|---|---|
| 1 | 3 | 2 | 3 | 4 |
| 2 | 2 | 4 | 5 | 3 |
| 3 | 6 | 7 | 1 | 4 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows × cols) time · O(rows × cols) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 2 | 2 | 3 |
|---|---|---|---|---|
| 1 | 3 | 2 | 3 | 4 |
| 2 | 2 | 4 | 5 | 3 |
| 3 | 6 | 7 | 1 | 4 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Example 2
| 0 | 3 | 3 |
|---|---|---|
| 1 | 3 | 3 |
Flat ground lets water wander anywhere, so every cell reaches both coasts.
Example 3
| 0 | 5 | 1 |
|---|---|---|
| 1 | 1 | 5 |