Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.

Note:

Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000.

Example:

Given the following 3x6 height map:
[
[1,4,3,1,3,2],
[3,2,1,3,2,4],
[2,3,3,2,3,1]
] Return 4.

The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.

After the rain, water is trapped between the blocks. The total volume of water trapped is 4.

Approach #1: C++. [priority_queue]

class Solution {
public:
int trapRainWater(vector<vector<int>>& heightMap) {
if (heightMap.size() == 0) return 0;
int row = heightMap.size(), col = heightMap[0].size();
vector<vector<int>> visited(row, vector<int>(col, 0));
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq; for (int i = 0; i < col; ++i) {
pq.push({heightMap[0][i], {0, i}});
pq.push({heightMap[row-1][i], {row-1, i}});
visited[0][i] = 1;
visited[row-1][i] = 1;
} for (int i = 1; i < row-1; ++i) {
pq.push({heightMap[i][0], {i, 0}});
pq.push({heightMap[i][col-1], {i, col-1}});
visited[i][0] = 1;
visited[i][col-1] = 1;
} int ans = 0;
int curMaxHeight = 0; while (!pq.empty()) {
pair<int, pair<int, int>> cur = pq.top();
pq.pop();
curMaxHeight = max(curMaxHeight, cur.first);
int x = cur.second.first, y = cur.second.second;
for (auto dir : dirs) {
int xx = x + dir.first;
int yy = y + dir.second;
if (judge(xx, yy, heightMap) && visited[xx][yy] == 0) {
pq.push({heightMap[xx][yy], {xx, yy}});
visited[xx][yy] = 1;
if (heightMap[xx][yy] < curMaxHeight) {
ans += curMaxHeight - heightMap[xx][yy];
}
}
}
} return ans;
} private:
vector<pair<int, int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static bool judge(int x, int y, vector<vector<int>>& heightMap) {
int m = heightMap.size();
int n = heightMap[0].size();
if (x < 0 || x >= m || y < 0 || y >= n) return false;
else return true;
}
};

  

Analysis:

The problem is very typical of this similar questions.

Firstly, we use a priority_queue to store the bordars cells.

Secondly, we access to the top-first elements and record the maximum height in the top-first elements from start to now.

Thirdly, traveling current top-first element's top, left, right and bottom cells, if the position if vaild and the cell's height is less then the maximum height, then we use maximum height to subtract the cell's value, and add the difference to the ans.

407. Trapping Rain Water 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 收集雨水之二

    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 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  4. [leetcode] 407. Trapping Rain Water II

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

  5. 407 Trapping Rain Water II 接雨水 II

    给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...

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

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

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

  9. (算法)Trapping Rain Water II

    题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...

随机推荐

  1. UVA548(二叉树遍历)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  2. 高效C#编码优化

    1.foreach VS for 语句 Foreach 要比for具有更好的执行效率Foreach的平均花费时间只有for的30%.通过测试结果在for和foreach都可以使用的情况下,我们推荐使用 ...

  3. 2015.3.2 VC++6制作非MFC dll以及VS2005、VS2010调用

    1.在VC6中新建工程,选择Win32 Dynamic-Link Libary,输入dll名称如 DLL2015 2.在类型选择中,选择第2项 A Simple Dll project OK 3.随后 ...

  4. CreateRemoteThread 远程dll注入

    1.dll中的内容 // dllmain.cpp : 定义 DLL 应用程序的入口点.#include "stdafx.h" BOOL APIENTRY DllMain( HMOD ...

  5. docker 笔记 (7) 限制容器

    内存 -m 或 --memory:设置内存的使用限额,例如 100M, 2G.--memory-swap:设置 内存+swap 的使用限额.--vm 1:启动 1 个内存工作线程.--vm-bytes ...

  6. ios配合iTuns提取应用Documents下的文件到本地

    出处:http://blog.csdn.net/jianandjan/article/details/50442988 有一些App需要通过使用iTunes让用户上传和下载文档.要让iOS程序支持iT ...

  7. 关于EF中实体和数据表以及查询语句映射的问题

    关于EF中实体和数据表以及查询语句映射的问题? 很多人在使用的时候分不清楚 实体字段应该少于等于(数据库中的表字段或者SQL查询中的临时字段).这样在查询或者添加修改都不会出现问题 如果实体的字段大于 ...

  8. hibernate的子查询

    hibernate原话 HQL supports subqueries in the where clause. We can't think of many good uses for subque ...

  9. springboot中单元测试

    测试service: 测试api:

  10. tornado带签名的cookie原理