42. Trapping Rain Water I && II
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
Analysis:
We first find out the max height in the array, then we start from the leftmost bar which is considered as the wall of the container. If there is a bar whose height is less than the wall, water will be saved above that bar. We do the same operation from rightmost to the highest bar position.
1 public class Solution { 2 public int trap(int[] height) { 3 if (height == null || height.length <= 2) return 0; 4 int maxIndex = 0; 5 for (int i = 1; i < height.length; i++) { 6 if (height[i] > height[maxIndex]) { 7 maxIndex = i; 8 } 9 } 10 int leftMax = height[0]; 11 int total = 0; 12 for (int i = 1; i < maxIndex; i++) { 13 if (height[i] < leftMax) { 14 total += (leftMax - height[i]); 15 } else { 16 leftMax = height[i]; 17 } 18 } 19 int rightMax = height[height.length - 1]; 20 for (int i = height.length - 2; i > maxIndex; i--) { 21 if (height[i] < rightMax) { 22 total += (rightMax - height[i]); 23 } else { 24 rightMax = height[i]; 25 } 26 } 27 return total; 28 } 29 }
Trapping Rain Water II
Given an m x n
matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.
Note:
Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000.
Example:
Given the following 3x6 height map: [ [1,4,3,1,3,2], [3,2,1,3,2,4], [2,3,3,2,3,1] ] Return 4.
The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
before the rain.
After the rain, water is trapped between the blocks. The total volume of water trapped is 4.
分析:
从四周出发,选取最低点(木桶原理),然后选取周围没有被visited的点。找到更低的点,则把当前点和低点的差值作为可以装水的量,注意,在加入新的点的时候,那个点的高度应该使用当前点的高度,这样我们就不用倒着回去找最高点了。
1 class Solution { 2 public int trapRainWater(int[][] heights) { 3 if (heights == null || heights.length == 0 || heights[0].length == 0) return 0; 4 5 PriorityQueue<Cell> queue = new PriorityQueue<>(1, (cell1, cell2) -> cell1.height - cell2.height); 6 int row = heights.length, col = heights[0].length; 7 boolean[][] visited = new boolean[row][col]; 8 9 // add border cells to the queue. 10 for (int i = 0; i < row; i++) { 11 visited[i][0] = true; 12 visited[i][col - 1] = true; 13 queue.offer(new Cell(i, 0, heights[i][0])); 14 queue.offer(new Cell(i, col - 1, heights[i][col - 1])); 15 } 16 17 for (int i = 0; i < col; i++) { 18 visited[0][i] = true; 19 visited[row - 1][i] = true; 20 queue.offer(new Cell(0, i, heights[0][i])); 21 queue.offer(new Cell(row - 1, i, heights[row - 1][i])); 22 } 23 24 // from the borders, pick the shortest cell visited and check its neighbors: 25 // if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped 26 // add all its neighbors to the queue. 27 int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 28 int res = 0; 29 while (!queue.isEmpty()) { 30 Cell cell = queue.poll(); 31 for (int[] dir : dirs) { 32 int neighbor_row = cell.row + dir[0]; 33 int neighbor_col = cell.col + dir[1]; 34 if (neighbor_row >= 0 && neighbor_row < row && neighbor_col >= 0 && neighbor_col < col && !visited[neighbor_row][neighbor_col]) { 35 visited[neighbor_row][neighbor_col] = true; 36 res += Math.max(0, cell.height - heights[neighbor_row][neighbor_col]); 37 queue.offer(new Cell(neighbor_row, neighbor_col, Math.max(heights[neighbor_row][neighbor_col], cell.height))); 38 } 39 } 40 } 41 return res; 42 } 43 } 44 45 class Cell { 46 int row; 47 int col; 48 int height; 49 public Cell(int row, int col, int height) { 50 this.row = row; 51 this.col = col; 52 this.height = height; 53 } 54 }