Practice/Meta/Leetcode 417. Pacific Atlantic Water Flow
CodingMust
You are given a rectangular elevation map represented as an m × n matrix of integers, where each cell contains the height above sea level at that position. Rain falls uniformly across the entire map.
Water can flow from any cell to its four adjacent neighbors (up, down, left, right) if the neighboring cell's height is less than or equal to the current cell's height.
The map is bordered by two river basins:
For simplicity, we group:
Your task is to find all cells where rainwater can flow to both Basin A and Basin B.
Return the result as a list of coordinates [row, col] in any order.
[row, col] pairsExample 1:
Input: 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]] Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] Explanation: Basin A (North/West) is at the top and left edges. Basin B (South/East) is at the bottom and right edges. The cells in the output can flow to both basins.
Example 2:
Input: heights = [[2,2],[2,2]] Output: [[0,0],[0,1],[1,0],[1,1]] Explanation: All cells have equal height, so water can flow in all directions, reaching both basins.
Example 3:
Input: heights = [[5]] Output: [[0,0]] Explanation: A single cell borders both basin groups.