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 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.
407. Trapping Rain Water II的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- [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 elevati ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [leetcode] 407. Trapping Rain Water II
https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...
- 407 Trapping Rain Water II 接雨水 II
给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- Leetcode: Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- (算法)Trapping Rain Water II
题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...
随机推荐
- bootstrap-table 父子表入门篇
官方文档:http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/#多语言 一.引入js.css <!-- 引入bootstrap ...
- linux下安装composer
在linux下使用comoser命令,但是提示composer command not found 那么就是当前环境中没有composer 学习源头: https://blog.csdn.net/gb ...
- Nginx解决错误413 Request Entity Too Large
最近一个项目当中,要求上传图片,并且限制图片大小,虽然在laravel当中已经添加了相关的表单验证来阻止文件过大的上传,然而当提交表单时,还没轮到laravel处理,nginx就先报错了.当你仔细看报 ...
- springboot springcloud eureka
参考: https://www.cnblogs.com/skyblog/p/5133752.htmlhttp://blog.csdn.net/u012734441/article/details/78 ...
- 【转】 Pro Android学习笔记(八四):了解Package(3):包间数据共享
目录(?)[-] 共享User ID的设置 共享资源例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...
- HTML 5中的结构元素
1.header:标记头部区域的内容 .footer:标记页脚区域的内容 .section:Web页面中的一块区域 4.article:独立的文章内容区域 5.aside:相关侧边内容或者引文区域 6 ...
- DBUtils使用BeanListHandler及BeanHandler时返回null
一.使用Bean相关方法时返回null 问题描述: 使用DBUtils查询数据,如果使用ArrayListHandler等都能够返回正确值,但使用BeanListHandler 和 BeanHandl ...
- Spring学习十 rest
1: Web service: 是一个大的概念范畴,它表现了一种设计思想 SOAP 是 Web service 的一个重要组成部份. SOAP 是一种协议而非详细产品.SOAP 是通过 XML ...
- 找不到引用microsoft.office.core解决办法 via mrcjiong
在控制面板中,选择"添加删除程序",找到office ,选择"更改",在对话框中选择"添加删除功能",然后选择自定义安装,添加上office ...
- 【转】js获取对象的所有属性和方法
//有时候需要知道一个js对像的所有属性和方法来帮助调试,下面是再网上找到的一个比较给力的方法 function displayProp(obj){ var names=""; f ...