[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 ...
随机推荐
- 使用 Python 创建你自己的 Shell(下)
导读 在上篇中,我们已经创建了一个 shell 主循环.切分了命令输入,以及通过 fork 和 exec 执行命令.在这部分,我们将会解决剩下的问题.首先,cd test_dir2 命令无法修改我们的 ...
- 在link的url里新增参数
(文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) <%= link_to image_tag("/images/icons/aaa. ...
- 为nginx增加nginx_http_concat模块
为nginx增加nginx_http_concat模块 时间 2013-06-05 22:14:56 我行我思 原文 http://www.fanjun.me/?p=562 主题 Nginx 缘由 ...
- Object-c 控制语句
控制语句: 分支语句 if-else 有控制机制 switch 循环语句 while do-while for 跳转语句 break,continue,goto
- AngularJS服务中serivce,factory,provider的区别
Angular服务是一个由服务工厂创建的单例对象.这些服务工厂是由 service provider 依次创建的.而service providers是构造函数.它们必须包含一个$get属性用于在实例 ...
- 【转】cas注册后自动登录
本文转自:http://denger.iteye.com/blog/805743 1. 关于CAS的介绍不再累述,我想涉及过SSO同学应该都会对该框架所有了解,我们目前项目采用的CAS Server ...
- mysql 我的学习
安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...
- JavaScript关闭窗口的同时打开新页面的方法
做网页的时候需要弹出一个小窗口,然后要实现一个功能就是鼠标点击超链接关闭小窗口并打开一个新页面,就如同下图: 这是一个小窗口,点击超链接这个窗口会关闭并且会正常在浏览器打开新页面,首先写js关闭窗口的 ...
- 将文件放到Android模拟器的SD卡
1.打开DDMS页面2.打开File Explorer页,如果没有,在Window –> Show View –>File Explorer3.一般就在mnt –> sdcard中4 ...
- 【动态规划】盖房子(house)--未提交--已提交
问题 D: 盖房子(house) 时间限制: 1 Sec 内存限制: 64 MB提交: 27 解决: 16[提交][状态][讨论版] 题目描述 FJ最近得到了面积为n*m的一大块土地,他想在这块土 ...