博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
407. Trapping Rain Water II
阅读量:5237 次
发布时间:2019-06-14

本文共 2676 字,大约阅读时间需要 8 分钟。

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
>& heightMap) { if (heightMap.size() == 0) return 0; int row = heightMap.size(), col = heightMap[0].size(); vector
> visited(row, vector
(col, 0)); priority_queue
>, vector
>>, greater
>>> 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
> 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
> dirs = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}}; static bool judge(int x, int y, vector
>& 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.

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/10151233.html

你可能感兴趣的文章
iOS: 数据持久化方案
查看>>
【C#】【Thread】Monitor和Lock
查看>>
UVALive - 3635 - Pie(二分)
查看>>
集合类List,set,Map 的遍历方法,用法和区别
查看>>
Scala入门系列(十):函数式编程之集合操作
查看>>
pulseaudio的交叉编译
查看>>
Cracking The Coding Interview 1.1
查看>>
vb.net 浏览文件夹读取指定文件夹下的csv文件 并验证,显示错误信息
查看>>
NetworkInterface的使用
查看>>
JQuery Ajax()方法
查看>>
元素自动居中显示
查看>>
JDBC 时间处理
查看>>
hadopp 环境搭建
查看>>
【2018】听懂你能看懂的句子
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
【BZOJ3052】【UOJ#58】【WC2013】糖果公园(树上莫队)
查看>>
荷兰国旗问题
查看>>