<p>Given an <code>m x n</code> integer matrix <code>heightMap</code> representing the height of each unit cell in a 2D elevation map, return <em>the volume of water it can trap after raining</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg" style="width: 361px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg" style="width: 401px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
<strong>Output:</strong> 10
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == heightMap.length</code></li>
	<li><code>n == heightMap[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= heightMap[i][j] &lt;= 2 * 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int trapRainWater(vector<vector<int>>& heightMap) {
        struct locHeight{
            int x, y, h;
        };
        if (heightMap.size() <= 1 || heightMap[0].size() <= 1) {
            return 0;
        }

        int m = heightMap.size(), n = heightMap[0].size();
        priority_queue<locHeight, vector<locHeight>, 
            decltype([] (auto loc1, auto loc2) { return loc1.h > loc2.h; })> pq;
        
        for (int i = 0; i < m; i++) {
            locHeight lht;
            lht.x = i;
            lht.y = 0;
            lht.h = heightMap[i][0];
            pq.push(lht);

            locHeight lht2;
            lht2.x = i;
            lht2.y = n-1;
            lht2.h = heightMap[i][n-1];
            pq.push(lht2);

            heightMap[i][0] = -1;
            heightMap[i][n-1] = -1;
        }
        

        for (int i = 1; i < n-1; i++) {
            locHeight lht;
            lht.x = 0;
            lht.y = i;
            lht.h = heightMap[0][i];
            pq.push(lht);

            locHeight lht2;
            lht2.x = m-1;
            lht2.y = i;
            lht2.h = heightMap[m-1][i];
            pq.push(lht2);

            heightMap[0][i] = -1;
            heightMap[m-1][i] = -1;
        }

        int level = pq.top().h, res = 0;
        while (!pq.empty()) {
            auto lht = pq.top();
            pq.pop();
            int x = lht.x, y = lht.y, h = lht.h;
            level = max(level, h);

            for (auto [i, j]: vector<pair<int, int>> {{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}}) {
                if (0 <= i && i < m && 0 <= j && j < n && heightMap[i][j] != -1) {
                    locHeight nxt;
                    nxt.x = i;
                    nxt.y = j;
                    nxt.h = heightMap[i][j];
                    pq.push(nxt);

                    if (nxt.h < level) {
                        res += level - nxt.h;
                    }
                    
                    heightMap[i][j] = -1;
                }
            }
        }
        return res;
    }
};
25131