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. jira python操作,自动创建问题

    jira web api地址 http://jira.**.com/plugins/servlet/restbrowser http://jira.**.com/rest/api/2/issue/cr ...

  2. 机器学习:PCA(基础理解、降维理解)

    PCA(Principal Component Analysis) 一.指导思想 降维是实现数据优化的手段,主成分分析(PCA)是实现降维的手段: 降维是在训练算法模型前对数据集进行处理,会丢失信息. ...

  3. [iOS]UIImageView增加圆角

    [iOS]UIImageView增加圆角 "如何给一个UIImageView增加圆角?有几种方法?各自区别?" 备注:本文参考自http://www.jianshu.com/p/d ...

  4. scrapy-redis源码抛析

    #scrapy-redis--->queue.py-->class FifoQueue 队列 LifoQueue(lastinfirstout栈) #self.server父类Base中链 ...

  5. DAY10-MYSQL表操作

    一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 http://www.cnblogs.com/guoyunlong666/p/8491702.html 二 表介绍 表 ...

  6. DAY9-python并发之多进程

    一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...

  7. Logstash-2.4.1的下载(图文详解)

    第一步:进入Elasticsearch的官网 https://www.elastic.co/ 第二步:点击downloads https://www.elastic.co/downloads 第三步: ...

  8. mybaits中date类型显示时分秒(orcle数据库)

    <insert id="insert" parameterType="daSysLoginLog"> insert into DA_SYS_LOGI ...

  9. Consumer设计-high/low Level Consumer

    1 Producer和Consumer的数据推送拉取方式   Producer Producer通过主动Push的方式将消息发布到Broker n Consumer Consumer通过Pull从Br ...

  10. spirng boot web配置开发

    spring-booter-starter-web是spring-boot web发开的核心,自动配置信息存储在spring-boot-autoconfigure.jar 下面的web目录里面,包含了 ...