Trapping Rain Water II

problem [https://leetcode.com/problems/trapping-rain-water-ii/ 407. Trapping Rain Water II].

Problem description
Given an  integer matrix   representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.


 * Example 1 Trapping Rain Water II Example 1.jpeg
 * Input: heightMap = 1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1
 * Output: 4
 * Explanation: After the rain, water is trapped between the blocks. We have two small pounds 1 and 3 units trapped. The total volume of water trapped is 4.


 * Example 2 Trapping Rain Water II Example 2.jpeg
 * Input: 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
 * Output: 10


 * Constraints

Problem analysis
We can see that if there is any water trapped in the area, the water will be trapped in one or more regions. Each region's water height is constrained by the cell on the region's boundary with lowest height. Also, all cells that can trap water cannot be on the edges or corners.

For these reasons, we need to keep track of the boundaries as we iterate through all the cells to find out how much amount of water each cell will trap. We also notice the need to find the boundary cell with smallest height, since the lowest-height cell on the boundary will directly determine the water level of its neighboring region. So the suitable data structure for this is the min-heap.

We also need to keep track of whether a cell has been visited, in order to avoid double processing of the same cell.

So we can create a  class:
 * The fields,  ,   come directly from input data and are immutable.
 * means the level of water after raining.
 * If  then   represents the computed level of water after rain.
 * Otherwise it means that the cell cannot trap water, or the amount of water it can trap is undecided.
 * means whether the cell has been visited during the iterations of computing water amounts.

The entire procedure will be as follows:
 * 1) Initialize   to 0.
 * 2) Create the m × n   matrix, with each item being an instance of class.
 * 3) Create the min-heap   to keep track of the boundary cells to work on.
 * 4) At the beginning of the iterations, mark all corners and edges as , and add all edges to the.
 * 5) While   is not empty:
 * 6) Pop the top of the heap, i.e. the cell   with minimum height.
 * 7)   is either 0 or the previously computed water height. We can let.
 * 8) Inspect each neighbor (up / down / left / right) of  . Add each neighbor not yet visited to the min-heap, mark it as , and set its  . This is because we want to always ensure that we still have a valid boundary after shrinking the boundaries, and that each single cell is processed exactly once.
 * 9) * If, then   cannot trap water.
 * 10) * Otherwise set . Then add   to.
 * 11) After processing all cells, return   as the result.

Complexity analysis

 * Time complexity
 * We have two major parts:
 * Creating the matrix of cells. Time complexity is O(m ⋅ n).
 * Each cell (excluding the corner cells) will be added once into the min-heap and popped once.
 * Insertion of single cell: O(log(m ⋅ n)).
 * Deletion of minimum height cell: O(1).
 * Generating neighbors: O(1).
 * So the total time complexity on heap operations is O(m ⋅ n ⋅ log(m ⋅ n)).
 * So the total time complexity is O(m ⋅ n ⋅ log(m ⋅ n)).


 * Space complexity
 * The space complexity for cell matrix is O(m ⋅ n).
 * The space complexity for the min heap is also O(m ⋅ n), since it can only contain at most as many items as in input.
 * So the total space complexity is O(m ⋅ n).