【Lintcode】364.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
.
题解:
之前的题是Two pointer, 本质是在给定的边界不断向中心遍历,这道题也是,不过边界由两端变成了四个边,同样是内缩遍历。而且这道题还需要堆的思想来从最小端开始遍历(防止漏水)。详见 here。
Solution 1 () (from here 转自Granyang)
class Solution {
public:
int trapRainWater(vector<vector<int> > &heightMap) {
if (heightMap.empty()) {
return ;
}
int m = heightMap.size(), n = heightMap[].size();
int res = , mx = INT_MIN;
priority_queue<pair<int, int>, vector<pair<int, int>>,greater<pair<int, int>>> q;
vector<vector<bool>> visited(m, vector<bool>(n, false));
vector<vector<int>> dir{{, -}, {-, }, {, }, {, }};
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (i == || i == m - || j == || j == n - ) {
q.push({heightMap[i][j], i * n + j});
visited[i][j] = true;
}
}
} while (!q.empty()) {
auto t = q.top();
q.pop();
int h = t.first, r = t.second / n, c = t.second % n;
mx = max(mx, h);
for (int i = ; i < dir.size(); ++i) {
int x = r + dir[i][], y = c + dir[i][];
if (x < || x >= m || y < || y >= n || visited[x][y] == true) continue;
visited[x][y] = true;
if (heightMap[x][y] < mx) res += mx - heightMap[x][y];
q.push({heightMap[x][y], x * n + y});
}
}
return res;
}
};
【Lintcode】364.Trapping Rain Water II的更多相关文章
- 【Lintcode】363.Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 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 ...
- [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】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
随机推荐
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
- 九度OJ 1345:XXX定律之画X (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:361 解决:157 题目描述: 给你一个n,然后让你输出F(n) 规则是这样的,F(n)的输出结果是: F(n-1) F(n-1) ...
- can not find java.util.map java.lang.Double问题
mybatis @Param注解和ParamType属性不能共存
- centos6安装nginx最详细步骤
第一步:在centos下面下载 nginx wget http://nginx.org/download/nginx-1.2.9.tar.gz 解压 tar zxf nginx-1. ...
- linux c编程:进程控制(二)_竞争条件
前面介绍了父子进程,如果当多个进程企图对共享数据进行处理.而最后的结果又取决于进程运行的顺序时,就认为发生了竞争关系.通过下面的例子来看下 在这里标准输出被设置为不带缓冲的,于是父子进程每输出一个字符 ...
- 关于highcharts-ng
1.内容都正确但是不显示,使用parseInt()方法转换
- 分析DNS解析时间
提高网页的打开速度,一般地我们会选择使用CDN,利用“就近原则”让用户在最短的时间内获取到服务器资源,实际应用当中采用CDN的方式提高网站访问速度的效果也是最明显的.这也就是为什么国外的空间打开速度远 ...
- iOS swift 语句只能写在函数体内
1. 语句只能在函数体内: eg 因为我写在playground里面没报错 我直接放在这个位置就报错了 在这个.swift 文件里面 print 应该写在func 等方法(函数)里面 其他语句 ...
- Oracle数据库体系结构(4)oracle控制文件
控制文件的概述 1.控制文件是oracle数据库非常重要的物理文件,描述了整个数据库的物理结构信息,包括数据库名称.数据文件与重做日志文件的名称与位置,日志序列号等信息.数据库实例根据初始化参数CON ...
- 继承、多态——成员变量、成员函数、构造函数(this、super)
继承 1.继承使用原因: 1.提高了代码的复用性 2.让类与类之间产生了关系,有了这个关系,才有了多态的特性 2.继承注意事项: 千万不要为了获取其他类的功能,简化代码而继承. 必须是类与类之间有所属 ...