https://leetcode.com/problems/trapping-rain-water-ii/

// https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue/2
// 这个解法真的好棒,真的太棒了
// 一维的也能搞定 // 利用一个PriorityQueue,从小往大排的 import java.util.Comparator;
import java.util.PriorityQueue; public class Solution { public class Cell {
int row;
int col;
int height;
public Cell(int r, int c, int h) {
row = r;
col = c;
height = h;
}
} public int trapRainWater(int[][] heightMap) {
if (heightMap == null || heightMap.length == 0 || heightMap[0].length == 0) {
return 0;
}
int rows = heightMap.length;
int cols = heightMap[0].length;
boolean [][]visited = new boolean[rows][cols];

     // 注意,是从小往大排
PriorityQueue<Cell> pq = new PriorityQueue<Cell>(1, new Comparator<Cell>(){
public int compare(Cell a, Cell b) {
return a.height - b.height;
}
}); for (int i=0; i<rows; i++) {
visited[i][0] = true;
visited[i][cols-1] = true;
pq.offer(new Cell(i, 0, heightMap[i][0]));
pq.offer(new Cell(i, cols-1, heightMap[i][cols-1]));
} for (int i=0; i<cols; i++) {
visited[0][i] = true;
visited[rows-1][i] = true;
pq.offer(new Cell(0, i, heightMap[0][i]));
pq.offer(new Cell(rows-1, i, heightMap[rows-1][i]));
} int result = 0;
int [][]dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
while (!pq.isEmpty()) {
Cell cl = pq.poll();
for (int i=0; i<4; i++) {
int r = cl.row + dirs[i][0];
int c = cl.col + dirs[i][1];
if (r < 0 || r >= rows || c < 0 || c >= cols || visited[r][c]) {
continue;
}
int newHt = heightMap[r][c];
if (heightMap[r][c] < cl.height) {
result += cl.height - heightMap[r][c];
newHt = cl.height;
}
pq.offer(new Cell(r, c, newHt));
visited[r][c] = true;
} } return result;
}
}

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] 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 收集雨水之二

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

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

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

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

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

  8. (算法)Trapping Rain Water II

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

  9. [LintCode] Trapping rain water II

    Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1  ...

  10. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

随机推荐

  1. ref:详解MYSQL数据库密码的加密方式及破解方法

    ref:https://blog.csdn.net/paul123456789io/article/details/53081921 MySQL数据库用户密码跟其它数据库用户密码一样,在应用系统代码中 ...

  2. WS快捷键

    打开文件: Ctrl + Shift + N 打开类: Ctrl + N 打开函数: Ctrl + F12 “超级”打开: 双击 shift,可以 search anywhere. 复制整行: Ctr ...

  3. 机器学习之路:python 特征降维 特征筛选 feature_selection

    特征提取: 特征降维的手段 抛弃对结果没有联系的特征 抛弃对结果联系较少的特征 以这种方式,降低维度 数据集的特征过多,有些对结果没有任何关系,这个时候,将没有关系的特征删除,反而能获得更好的预测结果 ...

  4. [CodeForces-585F]Digits of Number Pi

    题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...

  5. Elasticsearch中document的基础知识

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...

  6. SnackDown Longest Increasing Subsequences 构造题

    Longest Increasing Subsequences 题目连接: https://www.codechef.com/SNCKPA16/problems/MAKELIS Description ...

  7. 推荐C#网站、书籍、资源

    推荐博客: 极简的随笔 http://www.cnblogs.com/guwei4037/p/3499135.html 见证大牛成长之路的专栏 http://blog.csdn.net/shanyon ...

  8. 双频无线网安装设置(5g ) for linux

    为了在局域网实现远程wifi调试,例如调试需要图像数据传输,则需要搭建局域网5g无线网络. 1.硬件要求 a. TP-Link(型号:TL-WDR6500,AC1300双频无线路由器,支持5g,2.4 ...

  9. 多线程_java多线程环境下栈信息分析思路

    导读:Java多线程开发给程序带来好处的同时,由于多线程程序导致的问题也越来越多,而且对问题的查找和分析解决对于菜鸟程序原来是是件头疼的事.下面我就项目中使用多线程开发程序过程中遇到的问题做详细的分析 ...

  10. document.all理解

    The all collection includes one element object for each valid HTML tag. If a valid tag has a matchin ...