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 ...
随机推荐
- CentOS7网卡设置为桥接模式静态IP配置方法详解
备份网络文件 [root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/ [root@localhost network- ...
- 解决IDEA输入法不跟随
1.关掉idea后在idea的安装路径下把jre64文件夹删掉,或者重命名也行,如把jre64改成jre642: 2.升级jdk版本至jdk 8u45以上 3.把Java安装路径下的jre文件拷贝到I ...
- GWT实现平滑移动图片效果
在一些网站的首页上,顶部总会存在一些平滑移动的图片,一般用来投放广告或者业务介绍.多个图片只在一个区域展示,仅通过一些方法来不停的移动这个区域的图片来达到展示多个图片的目的.如果是普通的网页,使用Jq ...
- hihoCoder#1322(树的判定)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶点 M 条边的无向图 G ,判断 G 是不是一棵树. 输入 第一个是一个整数 T ,代表测试数据的组 ...
- SQL字符串拼接
不同的数据库,相应的字符串拼接方式不同,通过对比加深一下记忆. 一.MySQL字符串拼接 1.CONCAT函数 语法格式:CONCAT(char c1, char c2, ..., char cn) ...
- 管理react路由的history对象的插件history的使用介绍
本文介绍如何使用history插件管理浏览记录 history插件的使用 history这个插件可以方便管理你的浏览记录 cnpm install history --save import crea ...
- 问题:C# ToString("P");结果:c#中的常用ToString()方法总结
c#中的常用ToString()方法总结 很多类都重写了ToString方法, 导致很多类的tostring到底执行了什么,有哪些参数,都不清楚 对于int,double等的tostring: C ...
- 浅谈Android四大组建之一Service---Service的创建
Service是安卓四大组件之一,个人觉得Service的使用还是比较简单的额,可以理解为看不见的Activity,因为Service的使用和Activity十分接近.启动啊,生命周期等,都十分简单. ...
- matlab图片高清复制到visio
编辑→复制图窗→在visio中粘贴
- ionic 页面乱码
是文本编辑器的问题.文本编辑器保存时默认保存成Encoding:ANSI编码格式,保存成utf-8就好了