407 Trapping Rain Water II 接雨水 II
给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。
说明:
m 和 n 都是小于110的整数。每一个单位的高度都大于0 且小于 20000。
示例:
给出如下 3x6 的高度图:
[
[1,4,3,1,3,2],
[3,2,1,3,2,4],
[2,3,3,2,3,1]
]
返回 4。
详见:https://leetcode.com/problems/trapping-rain-water-ii/description/
C++:
class Solution {
public:
int trapRainWater(vector<vector<int>>& heightMap) {
if (heightMap.empty())
{
return 0;
}
int m = heightMap.size(), n = heightMap[0].size(), res = 0, 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{{0,-1},{-1,0},{0,1},{1,0}};
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i == 0 || i == m - 1 || j == 0 || j == n - 1)
{
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 = 0; i < dir.size(); ++i)
{
int x = r + dir[i][0], y = c + dir[i][1];
if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y])
{
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;
}
};
参考:https://www.cnblogs.com/grandyang/p/5928987.html
407 Trapping Rain Water II 接雨水 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 收集雨水 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 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- 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
https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...
- Trapping Rain Water I && II
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
随机推荐
- Pull方式解析XML文件
package com.pingyijinren.test; import android.content.Intent; import android.os.Handler; import andr ...
- [bzoj1059][ZJOI2007]矩阵游戏_二分图最大匹配
矩阵游戏 bzoj-1059 ZJOI-2007 题目大意:给定一个n*n的棋盘,上面有一些格子被染黑,剩下都是白色.你每次可以交换两列或者两行,问你能否通过一系列操作使得棋盘的主对角线上的格子全是黑 ...
- cogs——1215. [Tyvj Aug11] 冗余电网
1215. [Tyvj Aug11] 冗余电网 ★ 输入文件:ugrid.in 输出文件:ugrid.out 简单对比 时间限制:1 s 内存限制:128 MB TYVJ八月月赛提高组 ...
- 安卓常见错误Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. 导入新的 ...
- eclipse添加高版本tomcat问题
eclipse添加高版本tomcat会报错,提示无法匹配高版本的容器installation is expected 解决方法: 1.找到tomcat的lib目录下的catalina.jar包,用压缩 ...
- Ubuntu 16.04下SecureCRT无法输入中文的解决思路
说明:首先网上的方法基本都是不行的,别试了. 但是可以有弥补方案: 1.通过外界的软件编辑好中文,然后粘贴过去.虽然是多了一步,但是也可以输入中文. 2.关于这个问题应该是没有中文字体库导致的,可以尝 ...
- http转https
1.先用jdk生成证书 先跳转到jdk的bin目录下:E:\Program Files\Java\jdk1.8.0_91\bin>keytool -genkey -alias tomcat -k ...
- base64加解密字符串
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- 滚动载入server端内容——样例
网页代码例如以下 <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- AES加密算法的C++实现
摘要:作为新一代的加密标准,AES 旨在取代 DES(请看<DES加密算法的C++实现>),以适应当今分布式开放网络对数据加密安全性的要求.本文在分析了 AES 加密原理的基础上着重说明了 ...