The problem. A grid of land heights sits between two oceans: the Pacific touches its top and left edges, the Atlantic its bottom and right edges. Rain flows from a cell to a neighbour (up, down, left or right) that is no higher than it, and from any edge cell into the ocean beside it. Return every cell from which water can reach both oceans.
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]]
-> [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
The 5 at (2,2) flows left to the Pacific through 4 and 2,
and right to the Atlantic through 3 and 1.Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson