How to get the solution to 2-D "Trapping Rain Water" problem from 1-D case
First let's recall the solution to the 1-D "Trapping Rain Water" problem:\n\npublic int trap(int[] height) {\n    int res = 0, l = 0, r = height.length - 1;\n  
First let's recall the solution to the 1-D "Trapping Rain Water" problem:\n```\npublic int trap(int[] height) {\n    int res = 0, l = 0, r = height.length - 1;\n        \n    while (l < r) {\n        if (height[l] <= height[r]) {\n            if (l + 1 < r) {\n                res += Math.max(0, height[l] - height[l + 1]);\n                height[l + 1] = Math.max(height[l], height[l + 1]);\n            }\n                \n            l++;\n                \n        } else {\n            if (l < r - 1) {\n                res += Math.max(0, height[r] - height[r - 1]);\n                height[r - 1] = Math.max(height[r], height[r - 1]);\n            }\n                \n            r--;\n        }\n    }\n        \n    return res;\n}\n```\nThe code above is not optimized but it does show the most prominent features of the solution to the "Trapping Rain Water" problem:\n1. **Rain water "container"**:  To hold the rain water, we need some sort of "container", which will be defined by its boundary. \n\n2. [**Liebig's law of the minimum**](https://en.wikipedia.org/wiki/Liebig%27s_law_of_the_minimum): The maximum height of  water for current container is determined by the lowest part of its boundary.\n\n3. **Boundary replacement**: The above "law of the minimum" applies only to neighbors of the lowest part of current boundary. After that the lowest part will be replaced by its neighbors. The height of the new parts of the boundary will be the larger one of the old boundary height and the height from the elevation map.\n\nIn the 1-D case, the boundary of the container contains only two points, i.e., the left and right edges (l and r in the above code) from the elevation map. We then apply the "law of the minimum" to find the minimum height of the boundary (corresponding to the two cases involving comparing height[l]  and height[r]). After that we replace the old part of the boundary with its neighbors and continue until the container shrinks to a point.\n\nNow let's turn to the 2-D case. \n\nFirst, what will the boundary of the container look like? Apparently it will no longer just be two edges, but instead, an enclosed area. Since our input is a matrix, the initial boundary will be the four edges of the matrix, which forms a rectangle.\n\nSecond, how do we apply the "law of the minimum"? This involves finding the minimum of the boundary, which is no longer as straightforward as in the 1-D case due to the rectangular boundary. Fortunately we have a data structure called "PriorityQueue" which can help find the minimum in log(queue_size) time.\n\nLastly, how do we replace the boundaries? Keep in mind we have 2-D elevation map now so each cell can have at most four neighbors. Also there are possibilities that one cell is neighbor of multiple cells on the boundary so we need to keep track of which cell has been visited to avoid repetition.\n\nThe following is the java solution according to the above analyses:\n```\nint[][] dirs = new int[][] {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n    \npublic int trapRainWater(int[][] heightMap) {\n    int m = heightMap.length;\n    int n = (m == 0 ? 0 : heightMap[0].length);\n    int res = 0;\n        \n    PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);\n    boolean[][] visited = new boolean[m][n];\n        \n    for (int i = 0; i < m; i++) {\n        pq.offer(new int[] {i, 0, heightMap[i][0]});\n        pq.offer(new int[] {i, n - 1, heightMap[i][n - 1]});\n        visited[i][0] = visited[i][n - 1] = true;\n    }\n        \n    for (int j = 1; j < n - 1; j++) {\n        pq.offer(new int[] {0, j, heightMap[0][j]});\n        pq.offer(new int[] {m - 1, j, heightMap[m - 1][j]});\n        visited[0][j] = visited[m - 1][j] = true;\n    }\n        \n    while (!pq.isEmpty()) {\n        int[] cell = pq.poll();\n        \t\n        for (int[] d : dirs) {\n            int i = cell[0] + d[0], j = cell[1] + d[1];\n            if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j]) continue;\n            res += Math.max(0, cell[2] - heightMap[i][j]);\n            pq.offer(new int[] {i, j, Math.max(heightMap[i][j], cell[2])});\n            visited[i][j] = true;\n        }\n    }\n        \n    return res;\n}\n```\n\nThe element in the PriorityQueue is an array with three elements encoding the row number, column number and the height of the cell on the boundary. We first build the initial boundary by enqueuing all cells on the outer edges of the input matrix and mark them as visited. We then apply the "law of the minimum" by polling the cell with minimum height on the boundary. After obtaining the water contained for each of its neighbor, we replace it with its neighbor and form a new boundary. Again we continue until the container shrinks to a point.\n\nThe time complexity is O(mnlog(mn)) and space complexity is O(mn). Space complexity is straightforward. For time complexity, we can think as follows: each cell will be pushed into and popped out of the queue only once, which will cost O(log(queue_size)). So the total will be O(mnlog(queue_size)). The queue size is apparently limited by the total number of cells, which is mn. Therefore the total time complexity is O(mnlog(mn)).
[]