Solution
IntuitionThe problem can be visualized as trapping water in a 3D elevation map. The key idea is that water trapped at any position depends on the minimum bounda
# Intuition 
The problem can be visualized as trapping water in a 3D elevation map. The key idea is that water trapped at any position depends on the minimum boundary height surrounding that position. Therefore, a priority queue (min-heap) is suitable for tracking the boundary cells and determining the minimum height efficiently.

# Approach
<!-- Describe your approach to solving the problem. -->
- Treat the elevation map as a grid, where each cell has a height.
- Use a min-heap to simulate the process of water filling the grid:
    - Start by adding all boundary cells to the heap since they form the initial "walls" of the container.
    - Mark these boundary cells as visited to prevent revisiting them.
- Process the heap:
    - Pop the cell with the minimum height from the heap.
    - For each of its unvisited neighbors, calculate the water trapped:
       -  If the height of the current cell is greater than the neighbor’s height, water is trapped, and the trapped volume is added to the total.
        - Add the neighbor to the heap with a height equal to the maximum of the current cell's height and the neighbor's height.
        - Mark the neighbor as visited.
- Repeat until the heap is empty.
- The total water trapped is the sum of all trapped volumes.

# Complexity
- Time complexity:
**O(m×n⋅log(m×n))** m×n cells in the grid. Each cell is pushed and popped from the heap once, and heap operations take O(log(m×n)).

- Space complexity:
**O(m×n)** The visited matrix and the heap both require O(m×n) space.

# Code
```python3 []
from heapq import heappush, heappop
from typing import List

class Solution:
    def trapRainWater(self, heightMap: List[List[int]]) -> int:
        if not heightMap or not heightMap[0]:
            return 0
        
        m, n = len(heightMap), len(heightMap[0])
        visited = [[False] * n for _ in range(m)]
        heap = []
        
        # Add all boundary cells to the heap
        for i in range(m):
            for j in [0, n - 1]:
                heappush(heap, (heightMap[i][j], i, j))
                visited[i][j] = True
        
        for j in range(n):
            for i in [0, m - 1]:
                heappush(heap, (heightMap[i][j], i, j))
                visited[i][j] = True
        
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        trapped_water = 0
        while heap:
            height, x, y = heappop(heap)
            for dx, dy in directions:
                nx, ny = x + dx, y + dy
                if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny]:
                    trapped_water += max(0, height - heightMap[nx][ny])
                    heappush(heap, (max(height, heightMap[nx][ny]), nx, ny))
                    visited[nx][ny] = True
        
        return trapped_water
```
['Python3']