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.

Approach #1: C++. [priority_queue]

class Solution {
public:
int trapRainWater(vector<vector<int>>& heightMap) {
if (heightMap.size() == 0) return 0;
int row = heightMap.size(), col = heightMap[0].size();
vector<vector<int>> visited(row, vector<int>(col, 0));
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq; for (int i = 0; i < col; ++i) {
pq.push({heightMap[0][i], {0, i}});
pq.push({heightMap[row-1][i], {row-1, i}});
visited[0][i] = 1;
visited[row-1][i] = 1;
} for (int i = 1; i < row-1; ++i) {
pq.push({heightMap[i][0], {i, 0}});
pq.push({heightMap[i][col-1], {i, col-1}});
visited[i][0] = 1;
visited[i][col-1] = 1;
} int ans = 0;
int curMaxHeight = 0; while (!pq.empty()) {
pair<int, pair<int, int>> cur = pq.top();
pq.pop();
curMaxHeight = max(curMaxHeight, cur.first);
int x = cur.second.first, y = cur.second.second;
for (auto dir : dirs) {
int xx = x + dir.first;
int yy = y + dir.second;
if (judge(xx, yy, heightMap) && visited[xx][yy] == 0) {
pq.push({heightMap[xx][yy], {xx, yy}});
visited[xx][yy] = 1;
if (heightMap[xx][yy] < curMaxHeight) {
ans += curMaxHeight - heightMap[xx][yy];
}
}
}
} return ans;
} private:
vector<pair<int, int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static bool judge(int x, int y, vector<vector<int>>& heightMap) {
int m = heightMap.size();
int n = heightMap[0].size();
if (x < 0 || x >= m || y < 0 || y >= n) return false;
else return true;
}
};

  

Analysis:

The problem is very typical of this similar questions.

Firstly, we use a priority_queue to store the bordars cells.

Secondly, we access to the top-first elements and record the maximum height in the top-first elements from start to now.

Thirdly, traveling current top-first element's top, left, right and bottom cells, if the position if vaild and the cell's height is less then the maximum height, then we use maximum height to subtract the cell's value, and add the difference to the ans.

最新文章

  1. inittab 分析
  2. 算导Ch34. NP Complete
  3. Java数组实现五子棋功能
  4. mina变长帧处理
  5. [转]如何正确清理C盘
  6. Struts2中ModelDriven的使用
  7. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
  8. Java流读写
  9. 利用微信公众平台实现自动回复消息—java版
  10. PAT (Advanced Level) 1062. Talent and Virtue (25)
  11. Android开发艺术2之Activity的启动模式
  12. How To Use ggplot2
  13. 用KMP算法实现strStr()
  14. button JS篇ant Design of react之二
  15. ASP.NET - Validators
  16. FloatingWindow 悬浮窗开源项目总结
  17. [android] 帧布局
  18. zabbix 3.4 直接 发现端口并作存活监控(带服务名)
  19. FormatMessage函数
  20. mysql 的安装,密码及修改 ,权限,基础语句(增删改查)

热门文章

  1. 一行删除所有docker container的命令
  2. [转载]get_fs()和set_fs()
  3. Linux驱动中获取系统时间
  4. Oracle 存储过程学习笔记
  5. 转:MySQL Row Format(MySQL行格式详解)
  6. Oracle redo 日志损坏的几种情况下的恢复
  7. FreeType 管理字形
  8. phonegap中使用自带浏览器打开链接
  9. jsp与struts的区别
  10. css水平居中,竖直居中技巧(一)