407. Trapping Rain Water II
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的更多相关文章
- 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 收集雨水之二
		
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 收集雨水 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/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...
 - 407 Trapping Rain Water II 接雨水 II
		
给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...
 - [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 II
		
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
 - [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 ...
 - (算法)Trapping Rain Water II
		
题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...
 
随机推荐
- Erlang tool -- lager overload protection
			
log 这个事, 说大不大说小又不小. 大点的, 可以用scribe flume 这样的系统去做, 小点的, 也就打印一个调试信息而已. 在Erlang 中, log 这事情确实比较伤, error_ ...
 - Java-API:java.lang百科
			
ylbtech-Java-API:java.lang百科 java.lang是提供利用 Java 编程语言进行程序设计的基础类.最重要的类是Object(它是类层次结构的根)和 Class(它的实例表 ...
 - cpu上下文切换(下)
			
--怎么查看系统的上下文切换情况 过多的上下文切换,会把cpu时间消耗在寄存器.内核栈以及虚拟内存等数据的保存和恢复上,缩短进程真正运行的时间,成了系统性能大幅下降的一个元凶. 查看,使用vmstat ...
 - vuex的实用知识点
			
本文系统介绍vuex的全部内容 为什么用vuex 组件通信知道吧,相信很多同学,刚学的时候很难懂,实时上在实际应用中,大型项目如果使用最原始的组件通信会非常的麻烦,主要体现在多层嵌套的组件之间的通信, ...
 - java常用八大排序法
			
最近查资料发现java排序挺有意思的,其中包含常见八种具有代表性的排序法:笔者觉得排序的功能重要,但更重要的是排序的思想:所以简单叙述一下常见排序方法名称,并用代码举例. A.插入排序(直接插入排序. ...
 - springmvc 注解式开发 接收请求参数
			
1.校正请求参数名: 2.以对象形式整体接收 3.路径变量:
 - 关于android通过shell修改文件权限的学习
			
首先是文件的读写属性(下图): 要通过shel命令l修改文件权限: 1.首先在cmd里输入adb shell 命令进入编辑模式 2.用cd命令进入到想要修改的文件目录,不知道的时候可以用ls 命令列表 ...
 - 文本框控件JTextField和JTextArea的使用
			
-----------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestTextF ...
 - Apollo问题
			
1.安装问题: 一不小心安装了NVIDIA,导致bash docker/scripts/dev_start.sh无法启动:[ERROR] Failed to start docker containe ...
 - QT5环境搭建
			
https://blog.csdn.net/liang19890820/article/details/53931813