Leetcode 407. Trapping Rain Water II Leetcode 42. Trapping Rain Water

Problem:

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.

Solution:

  终于来到了赫赫有名的Trapping Rain Water II,和Leetcode 42. Trapping Rain Water完全不同,这道题使用了优先级队列加BFS,难度非常高,反正我是做不出来,这篇博客讲的非常详细了,我就引用他的博客吧。当时我想了一种效率非常低下的方法,仅供思维拓展,就是从顶部开始,每次取一层,小于该高度全部设为0,大于等于该高度设为1,计算这个01矩阵所有低凹处的体积,直到最底层为止,这样做有一个很大的缺陷,如果矩阵的最高值和最小值相差极大,效率就非常低。

Code:

 1 class Solution {
 2 public:
 3     int trapRainWater(vector<vector<int>>& heightMap) {
 4         int m = heightMap.size();
 5         if(m == 0) return 0;
 6         int n = heightMap[0].size();
 7         if(n == 0) return 0;
 8         priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
 9         vector<vector<bool>> visited(m,vector<bool>(n,false));
10         for(int i = 0;i < m;++i){
11             visited[i][0] = true;
12             visited[i][n-1] = true;
13             pq.push(make_pair(heightMap[i][0],i*n));
14             pq.push(make_pair(heightMap[i][n-1],i*n+n-1));
15         }
16         for(int j = 1;j < n-1;++j){
17             visited[0][j] = true;
18             visited[m-1][j] = true;
19             pq.push(make_pair(heightMap[0][j],j));
20             pq.push(make_pair(heightMap[m-1][j],(m-1)*n+j));
21         }
22         int result = 0;
23         int height = 0;
24         while(!pq.empty()){
25             height = max(pq.top().first,height);
26             int x = pq.top().second/n;
27             int y = pq.top().second%n;
28             pq.pop();
29             if(x-1 >= 0 && !visited[x-1][y]){
30                 pq.push(make_pair(heightMap[x-1][y],(x-1)*n+y));
31                 visited[x-1][y] = true;
32                 result += ((height-heightMap[x-1][y]) > 0) ? (height-heightMap[x-1][y]) : 0;
33             }
34             if(x+1 < m && !visited[x+1][y]){
35                 pq.push(make_pair(heightMap[x+1][y],(x+1)*n+y));
36                 visited[x+1][y] = true;
37                 result += ((height-heightMap[x+1][y]) > 0) ? (height-heightMap[x+1][y]) : 0;
38             }
39             if(y-1 >= 0 && !visited[x][y-1]){
40                 pq.push(make_pair(heightMap[x][y-1],x*n+y-1));
41                 visited[x][y-1] = true;
42                 result += ((height-heightMap[x][y-1]) > 0) ? (height-heightMap[x][y-1]) : 0;
43             }
44             if(y+1 < n && !visited[x][y+1]){
45                 pq.push(make_pair(heightMap[x][y+1],x*n+y+1));
46                 visited[x][y+1] = true;
47                 result += ((height-heightMap[x][y+1]) > 0) ? (height-heightMap[x][y+1]) : 0;
48             }
49         }
50         return result;
51     }
52 };

Problem:

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.

Solution:

  终于来到了赫赫有名的Trapping Rain Water II,和Leetcode 42. Trapping Rain Water完全不同,这道题使用了优先级队列加BFS,难度非常高,反正我是做不出来,这篇博客讲的非常详细了,我就引用他的博客吧。当时我想了一种效率非常低下的方法,仅供思维拓展,就是从顶部开始,每次取一层,小于该高度全部设为0,大于等于该高度设为1,计算这个01矩阵所有低凹处的体积,直到最底层为止,这样做有一个很大的缺陷,如果矩阵的最高值和最小值相差极大,效率就非常低。

Code:

 1 class Solution {
 2 public:
 3     int trapRainWater(vector<vector<int>>& heightMap) {
 4         int m = heightMap.size();
 5         if(m == 0) return 0;
 6         int n = heightMap[0].size();
 7         if(n == 0) return 0;
 8         priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
 9         vector<vector<bool>> visited(m,vector<bool>(n,false));
10         for(int i = 0;i < m;++i){
11             visited[i][0] = true;
12             visited[i][n-1] = true;
13             pq.push(make_pair(heightMap[i][0],i*n));
14             pq.push(make_pair(heightMap[i][n-1],i*n+n-1));
15         }
16         for(int j = 1;j < n-1;++j){
17             visited[0][j] = true;
18             visited[m-1][j] = true;
19             pq.push(make_pair(heightMap[0][j],j));
20             pq.push(make_pair(heightMap[m-1][j],(m-1)*n+j));
21         }
22         int result = 0;
23         int height = 0;
24         while(!pq.empty()){
25             height = max(pq.top().first,height);
26             int x = pq.top().second/n;
27             int y = pq.top().second%n;
28             pq.pop();
29             if(x-1 >= 0 && !visited[x-1][y]){
30                 pq.push(make_pair(heightMap[x-1][y],(x-1)*n+y));
31                 visited[x-1][y] = true;
32                 result += ((height-heightMap[x-1][y]) > 0) ? (height-heightMap[x-1][y]) : 0;
33             }
34             if(x+1 < m && !visited[x+1][y]){
35                 pq.push(make_pair(heightMap[x+1][y],(x+1)*n+y));
36                 visited[x+1][y] = true;
37                 result += ((height-heightMap[x+1][y]) > 0) ? (height-heightMap[x+1][y]) : 0;
38             }
39             if(y-1 >= 0 && !visited[x][y-1]){
40                 pq.push(make_pair(heightMap[x][y-1],x*n+y-1));
41                 visited[x][y-1] = true;
42                 result += ((height-heightMap[x][y-1]) > 0) ? (height-heightMap[x][y-1]) : 0;
43             }
44             if(y+1 < n && !visited[x][y+1]){
45                 pq.push(make_pair(heightMap[x][y+1],x*n+y+1));
46                 visited[x][y+1] = true;
47                 result += ((height-heightMap[x][y+1]) > 0) ? (height-heightMap[x][y+1]) : 0;
48             }
49         }
50         return result;
51     }
52 };

猜你喜欢

转载自www.cnblogs.com/haoweizh/p/10234090.html