Given an m
x n
2D matrix
representing an island bordered by the two different oceans, one on the left and top edges and another on the right and bottom edges, determine which cells can allow rainwater to flow to both oceans. Each cell contains an integer representing its height above sea level.
Water can flow from one cell to another if the adjacent cell’s height is less than or equal to the current cell's height. Adjacent cells refers to cells directly north, south, west, and east of the a cell.
Return a list of all cells from which water can flow to both oceans in sorted order, based on their position when traversing the matrix from the top-left to the bottom-right
matrix: number[][]
: A 2D array of integersInput: matrix = [[1,2,3,1],[4,3,3,4],[5,2,4,3],[5,2,1,5],[1,5,1,3]]Output: [[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,2],[3,0],[4,0],[4,1]]Explanation: The listed cells are capable of reaching both oceans. For instance, the cell at (row 0, column 2) can flow directly to the top left ocean, and then from (row 0, column 2) it can reach the bottom right ocean through (row 0, column 3).
Input: matrix = [[1]]Output: [[0,0]]Explanation: The only cell can flow to both oceans.
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]Output: [[0,2],[1,2],[2,0],[2,1],[2,2]]Explanation: The listed cells are capable of reaching both oceans
m
, n
<= 100matrix[row][col]
<= 100,000Given an m
x n
2D matrix
representing an island bordered by the two different oceans, one on the left and top edges and another on the right and bottom edges, determine which cells can allow rainwater to flow to both oceans. Each cell contains an integer representing its height above sea level.
Water can flow from one cell to another if the adjacent cell’s height is less than or equal to the current cell's height. Adjacent cells refers to cells directly north, south, west, and east of the a cell.
Return a list of all cells from which water can flow to both oceans in sorted order, based on their position when traversing the matrix from the top-left to the bottom-right
matrix: number[][]
: A 2D array of integersInput: matrix = [[1,2,3,1],[4,3,3,4],[5,2,4,3],[5,2,1,5],[1,5,1,3]]Output: [[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,2],[3,0],[4,0],[4,1]]Explanation: The listed cells are capable of reaching both oceans. For instance, the cell at (row 0, column 2) can flow directly to the top left ocean, and then from (row 0, column 2) it can reach the bottom right ocean through (row 0, column 3).
Input: matrix = [[1]]Output: [[0,0]]Explanation: The only cell can flow to both oceans.
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]Output: [[0,2],[1,2],[2,0],[2,1],[2,2]]Explanation: The listed cells are capable of reaching both oceans
m
, n
<= 100matrix[row][col]
<= 100,000console.log()
statements will appear here.