Problem Explanation
The problem is asking us to find the volume of water that can be trapped in a 2D elevation map after raining. The elevation map is represented by an m x n integer matrix heightMap, where each cell represents the height of the unit cell.
The goal is to calculate the total volume of water that can be trapped between the blocks in the elevation map.
Solution Overview
The solution uses a priority queue to keep track of the cells with the lowest height. The algorithm starts by adding all the cells on the border of the elevation map to the priority queue. Then, it iteratively dequeues the cell with the lowest height from the queue and checks its neighboring cells. If a neighboring cell has a lower height, it calculates the volume of water that can be trapped and adds it to the total volume.
Step-by-Step Solution
- Initialize the variables:
m and n to store the number of rows and columns in the elevation map.
vis to store the visited cells.
pq to store the priority queue.
- Add all the cells on the border of the elevation map to the priority queue.
- Initialize the total volume of water that can be trapped to 0.
- Iterate until the priority queue is empty:
- Dequeue the cell with the lowest height from the queue.
- Check its neighboring cells:
- If a neighboring cell has a lower height, calculate the volume of water that can be trapped and add it to the total volume.
- Mark the neighboring cell as visited and add it to the priority queue with the maximum height of the current cell and the neighboring cell.
Code Explanation
The code implements the above steps using a priority queue. The trapRainWater function takes the elevation map as input and returns the total volume of water that can be trapped.
class Solution {
/**
* Calculates the total volume of water that can be trapped in a 2D elevation map.
* @param {number[][]} heightMap - The elevation map represented by an m x n integer matrix.
* @return {number} The total volume of water that can be trapped.
*/
trapRainWater(heightMap) {
// Get the number of rows and columns in the elevation map
const m = heightMap.length;
const n = heightMap[0].length;
// Initialize the visited cells
const vis = Array.from({ length: m }, () => Array(n).fill(false));
// Initialize the priority queue
const pq = new MinPriorityQueue({ priority: (a, b) => a[0] - b[0] });
// Add all the cells on the border of the elevation map to the priority queue
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (i === 0 || i === m - 1 || j === 0 || j === n - 1) {
pq.enqueue([heightMap[i][j], i, j]);
vis[i][j] = true;
}
}
}
// Initialize the total volume of water that can be trapped
let ans = 0;
// Define the directions for neighboring cells
const dirs = [-1, 0, 1, 0, -1];
// Iterate until the priority queue is empty
while (pq.size() > 0) {
// Dequeue the cell with the lowest height from the queue
const p = pq.dequeue().element;
// Check its neighboring cells
for (let k = 0; k < 4; ++k) {
const x = p[1] + dirs[k];
const y = p[2] + dirs[k + 1];
// Check if the neighboring cell is within the bounds and not visited
if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y]) {
// Calculate the volume of water that can be trapped
ans += Math.max(0, p[0] - heightMap[x][y]);
// Mark the neighboring cell as visited
vis[x][y] = true;
// Add the neighboring cell to the priority queue with the maximum height
pq.enqueue([Math.max(p[0], heightMap[x][y]), x, y]);
}
}
}
// Return the total volume of water that can be trapped
return ans;
}
}
// Priority queue implementation
class MinPriorityQueue {
constructor({ priority }) {
this.priority = priority;
this.elements = [];
}
// Enqueue an element
enqueue(element) {
this.elements.push(element);
this.elements.sort((a, b) => this.priority(a, b));
}
// Dequeue an element
dequeue() {
return this.elements.shift();
}
// Get the size of the queue
size() {
return this.elements.length;
}
}