给定一个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的更多相关文章

  1. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. [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 ...

  3. [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 ...

  4. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  5. 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 ...

  6. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  7. Trapping Rain Water I && II

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [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 ...

  9. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

随机推荐

  1. 【IntelliJ 】设置 IntelliJ IDEA 主题和字体的方法

    2 主题修改 2.1 界面主题修改 如上图所示,依次点击Files -> Settings,进入如下界面: 标注1:主题选择区: 标注2:Darcula.IntelliJ 和 Windows,三 ...

  2. MongoDB小结15 - find【查询条件$ne】

    $ne表示不相等 db.user.find({"name":{"$ne":"william"}})

  3. how to read openstack code : routes

    When coding a web system, you have to think about an important problem, how to map urls to logic. Op ...

  4. outlook pst 过大,要立刻处理

    用鼠標右鍵點擊Email Account,選擇“資料檔案屬性” 進入內容,再點擊“資料夾大小”, “全部大小”不要大於20,480,000KB(20GB), 如果超過請立即分拆此pst檔案, 或者點擊 ...

  5. Ios开发之 -- js和ios的交互

    ==WebViewJavascriptBridge的介绍== #下载:https://github.com/marcuswestin/WebViewJavascriptBridge #关于WebVie ...

  6. 怎样把引用的jar包和本项目一起导出成jar文件

    之所以要导出Runnable JAR.是由于我们希望将引用到的Jar包与本项目一起进行导出,所以不要选Jar file 选File/Export...然后Java/Runnable JAR file, ...

  7. Node.js创建自签名的HTTPS服务器

    https://cnodejs.org/topic/54745ac22804a0997d38b32d  用Node.js创建自签名的HTTPS服务器  发布于 4 年前  作者 eeandrew  6 ...

  8. VBS 操作Word

    VBS 操作Word   1.新建Word文档 '使用Add方法 Dim ObjWD,ObjDOC Set ObjWD=CreateObject("Word.application" ...

  9. MySQL-PREPARE语句

    MySQL-PREPARE语句 功能介绍: MySQL准备语句用法 为了使用MySQL准备语句,您需要使用其他三个MySQL语句如下: PREPARE - 准备执行的声明. EXECUTE - 执行由 ...

  10. 【Espruino】NO.13 蓝牙模块

    http://blog.csdn.net/qwert1213131/article/details/31830809 本文属于个人理解,能力有限.纰漏在所难免,还望指正! [小鱼有点电] [Espru ...