#22HardHeap & Breadth-First Search· Min-Heap Flood Fill from the Boundary
Trapping Rain Water II
Input format
heightMap: m x n matrix of non-negative integers
Output format
Integer: total trapped water volume
Examples
Input: heightMap = [[3, 3, 3], [3, 1, 3], [3, 3, 3]]
Output: 2
The centre cell is surrounded by walls of height 3, so it fills from 1 to 3 and holds 2 units.
Input: heightMap = [[5, 5, 5, 5], [5, 1, 1, 5], [5, 1, 5, 5], [5, 5, 5, 5]]
Output: 12
The three interior cells of height 1 are enclosed by height-5 blocks and each holds 4 units.
Input: heightMap = [[4, 4, 4, 4], [4, 0, 2, 4], [4, 4, 1, 4]]
Output: 2
The cell of height 0 can drain through its neighbour of height 2 and then the border cell of height 1, so it only fills to level 2.
Constraints
- 1 <= m, n <= 200
- 0 <= heightMap[i][j] <= 2 * 10^4
Study first
- #heap
- #breadth-first-search
- #matrix
- #priority-queue
