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. ASL测试 课题测试博客

    已知线性表具有元素{5,13,19,21,37,56,64,75,80,88,92},如果使用折半查找法,ASL是多少? 知识点1: 折半查找法:折半查找,又称作二分查找.这个查找的算法的特点,要求数 ...

  2. MXNet 中的几个数据集

    from mxnet import gluon def transform(data, label): return data.astype('float32') / 255., label.asty ...

  3. 有了这套flex页面布局方案,页面什么的,那都不是事。

    一.CSS3弹性盒子弹性盒子是CSS3的一种新布局模式.CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局 ...

  4. Java反射机制demo(六)—获得并操作一个类的属性

    Java反射机制demo(六)—获得并操作一个类的属性 获得并操作一个类的属性?! 不可思议啊,一个类的属性一般都是私有成员变量啊,private修饰符啊! 但是毫无疑问,这些东西在Java的反射机制 ...

  5. python 与 mongodb的交互

  6. c#程序员机试题

    一.题目: 有一数组: int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32}; 1.求出最大值 2.按每个数字的10位数分组(说明:0~ ...

  7. http常见请求头与响应头

    1.HTTP常见的请求头 If-Modified-Since:把浏览器端缓存页面的最后修改时间发送到服务器去,服务器会把这个时间与服务器上实际文件的最后修改时间进行对比.如果时间一致,那么返回304, ...

  8. 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 456  Solved: 215[Submit][Stat ...

  9. 几个常用的Eclipse插件

    用Eclipse Neon做ROS开发需要几个常用的插件,可以大大加速开发的进度. 1.常用插件 a.CMake Editer 地址:http://cmakeed.sourceforge.net/ec ...

  10. git服务端和客户端百度网盘下载地址

    https://pan.baidu.com/s/1BKw-bgYOrQjLkwUMzyH7KQ