Gamified challenge · Reverse DFS
heights[r][c] is the height of the island at row r, column c. The west ocean touches the top row and the left column; the east ocean touches the bottom row and the right column. Rain on a cell can flow up, down, left or right to a neighbour that is no higher (equal heights are fine), and from any edge cell straight into the ocean beside it.
Write bothOceans(heights) that returns every cell whose rain can reach both oceans, as a list of [row, col] pairs in any order, each cell once.
Example: [[1, 2, 2, 3], [3, 2, 3, 4], [2, 4, 5, 3], [6, 7, 1, 4]] → [[0, 3], [1, 3], [2, 2], [3, 0], [3, 1]].
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google