Leetcode: 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 are trapped between the blocks. The total volume of water trapped is 4.

Refer to https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue/2

这里有具体的例子:http://www.cnblogs.com/grandyang/p/5928987.html

Analysis, 根据木桶原理,先找外围最矮的bar,里边如果有bar比它还矮,一定能存水(因为四周所有的bar都比它高)

注意还可能存更多的水,因为往里面,很可能cell高度变化。所以要把BFS中间遇到的高的bar都存进queue,随着水平面提升,提升到这些bar的高度,看能不能有凹槽存更多的水

 1 public class Solution {
 2     public class Cell {
 3         int row;
 4         int col;
 5         int height;
 6         public Cell(int x, int y, int val) {
 7             this.row = x;
 8             this.col = y;
 9             this.height = val;
10         }
11     }
12
13     public int trapRainWater(int[][] heightMap) {
14         if (heightMap==null || heightMap.length<=2 || heightMap[0].length<=2) return 0;
15         int m = heightMap.length;
16         int n = heightMap[0].length;
17         int res = 0;
18         PriorityQueue<Cell> queue = new PriorityQueue<Cell>(1, new Comparator<Cell>() {
19             public int compare(Cell c1, Cell c2) {
20                 return c1.height - c2.height;
21             }
22         });
23         HashSet<Integer> visited = new HashSet<Integer>();
24         for (int i=0; i<m; i++) {
25             queue.offer(new Cell(i, 0, heightMap[i][0]));
26             queue.offer(new Cell(i, n-1, heightMap[i][n-1]));
27             visited.add(i*n+0);
28             visited.add(i*n+n-1);
29         }
30         for (int j=0; j<n; j++) {
31             queue.offer(new Cell(0, j, heightMap[0][j]));
32             queue.offer(new Cell(m-1, j, heightMap[m-1][j]));
33             visited.add(0*n+j);
34             visited.add((m-1)*n+j);
35         }
36         int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
37         while (!queue.isEmpty()) {
38             Cell cur = queue.poll();
39             for (int[] dir : directions) {
40                 int row = cur.row + dir[0];
41                 int col = cur.col + dir[1];
42                 if (row>=0 && row<m && col>=0 && col<n && !visited.contains(row*n+col)) {
43                     visited.add(row*n+col);
44                     res += Math.max(0, cur.height - heightMap[row][col]);
45                     queue.offer(new Cell(row, col, Math.max(cur.height, heightMap[row][col])));
46                 }
47             }
48         }
49         return res;
50     }
51 }
时间: 2024-12-23 18:46:13

Leetcode: Trapping Rain Water II的相关文章

[LeetCode] 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 th

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: 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. For example, Given [0,1,0,2,1,0,1,3,

[LeetCode] [Trapping Rain Water 2012-03-10]

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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

LeetCode: Trapping Rain Water [041]

[题目] 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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented

[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II

问题描述 给定一个二维矩阵,每个元素都有一个正整数值,表示高度.这样构成了一个二维的.有高度的物体.请问该矩阵可以盛放多少水? 相关题目:POJ The Wedding Juicer Description Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows: * A flat board of size W cm

Leetcode 407. 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 tha

LeetCode: Trapping Rain Water 解题报告

https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven 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. For example, Given [0,1,0,

LeetCode——Trapping Rain Water

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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a