[LintCode] 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.
Given 5*4
matrix
[12,13,0,12]
[13,4,13,12]
[13,8,10,12]
[12,13,12,12]
[13,13,13,13]
return 14
.
struct MyPoint{
int x, y, h;
MyPoint(int xx, int yy, int hh):x(xx), y(yy), h(hh){}
}; struct Cmp{
bool operator()(const MyPoint &p1, const MyPoint &p2){
return p1.h > p2.h;
}
}; class Solution {
public:
/**
* @param heights: a matrix of integers
* @return: an integer
*/
int trapRainWater(vector<vector<int> > &heights) {
int m = heights.size();
if(m < ) return ;
int n = heights[].size();
if(n < ) return ; int waterNum = ;
vector<vector<bool> > visit(m, vector<bool>(n, false));
priority_queue<MyPoint, vector<MyPoint>, Cmp> MyPointQue; for(int i = ;i < n;++i){
MyPointQue.push(MyPoint(, i, heights[][i]));
MyPointQue.push(MyPoint(m - , i, heights[m - ][i]));
visit[][i] = true;
visit[m - ][i] = true;
} for(int j = ;j < m - ;++j){
MyPointQue.push(MyPoint(j, , heights[j][]));
MyPointQue.push(MyPoint(j, n - , heights[j][n - ]));
visit[j][] = true;
visit[j][n - ] = true;
} const int detX[] = {, , , -}, detY[] = {, -, , };
while(MyPointQue.size() > ){
MyPoint p = MyPointQue.top();
MyPointQue.pop(); for(int i = ;i < ;++i){
int xx = p.x + detX[i], yy = p.y + detY[i];
if(xx < || xx >= m || yy < || yy >= n) continue;
if(visit[xx][yy]) continue;
else{
int h = heights[xx][yy];
if(heights[xx][yy] < p.h){
h = p.h;
waterNum += p.h - heights[xx][yy];
}
MyPointQue.push(MyPoint(xx, yy, h));
visit[xx][yy] = true;
}
}
}
return waterNum;
}
};
[LintCode] 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] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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: 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 ...
- 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 ...
- 【Lintcode】364.Trapping Rain Water II
题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...
随机推荐
- Merge Sort
#include<stdlib.h> #include<stdio.h> void Merge( int source[] , int temp[] , int start , ...
- 黑色30s高并发IIS设置
在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释. “黑色30秒”问题现象的主要特征是:排队的请求(Requests Queued)突增 ...
- The server does not support version 3.0 of the J2EE Web module specification
1.错误: 在eclipse中使用run->run on server的时候,选择tomcat6会报错误:The server does not support version 3.0 of t ...
- poj3259 bellman——ford Wormholes解绝负权问题
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35103 Accepted: 12805 Descr ...
- 破解php-screw加密过的文件有效方法
今天终于搞定更改过密钥的php-screw解密问题,乐呵一下! 改进下 这样就可以解密任何加密过的PHP源码(包括更改过密钥的),解密的原理稍后具体列出,先说下如何加密 列出之前写使用php scre ...
- CentOS 关闭蜂鸣器声音
也许你会遇到像我这样的情况,每次使用Linux终端,当听到发出“嘀嘀”的声音时候,我都有种把我的机箱拆掉把那个内置的蜂鸣装置拽下来的冲动.按 Tab时候“嘀嘀”,按空格时候“嘀嘀”,每个在vi中错误的 ...
- XPath 定位----光荣之路
被测试网页的HMTL代码 <html> <body> <div id="div1"> <input name="div1inpu ...
- linux shell脚本守护进程监控svn服务
最近搭建的svn服务不知道什么原因服务总是被关闭(如果你不知道怎么搭建svn可以参考linux下搭建svn版本控制软件),因此用shell脚本实现一个守护进程.用于监控svn服务是否启动,如果服务不在 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 6个朋友(codevs 2832)
2832 6个朋友 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有这么一种说法:认识6个人,你就认识全世 ...