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 are trapped between the blocks. The total volume of water trapped is 4.

Refer to https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue/2

这里有具体的例子:http://www.cnblogs.com/grandyang/p/5928987.html

Analysis, 根据木桶原理,先找外围最矮的bar,里边如果有bar比它还矮,一定能存水(因为四周所有的bar都比它高)

注意还可能存更多的水,因为往里面,很可能cell高度变化。所以要把BFS中间遇到的高的bar都存进queue,随着水平面提升,提升到这些bar的高度,看能不能有凹槽存更多的水

44-45行逻辑就是

if (height[row][col] < cur) {

  res += cur.height- height[row][col];

  queue.offer(new Cell(row, col, cur.height));

}

else {

  queue.offer(new Cell(row, col, height[row][col]));

}

 public class Solution {
public class Cell {
int row;
int col;
int height;
public Cell(int x, int y, int val) {
this.row = x;
this.col = y;
this.height = val;
}
} public int trapRainWater(int[][] heightMap) {
if (heightMap==null || heightMap.length<=2 || heightMap[0].length<=2) return 0;
int m = heightMap.length;
int n = heightMap[0].length;
int res = 0;
PriorityQueue<Cell> queue = new PriorityQueue<Cell>(1, new Comparator<Cell>() {
public int compare(Cell c1, Cell c2) {
return c1.height - c2.height;
}
});
HashSet<Integer> visited = new HashSet<Integer>();
for (int i=0; i<m; i++) {
queue.offer(new Cell(i, 0, heightMap[i][0]));
queue.offer(new Cell(i, n-1, heightMap[i][n-1]));
visited.add(i*n+0);
visited.add(i*n+n-1);
}
for (int j=0; j<n; j++) {
queue.offer(new Cell(0, j, heightMap[0][j]));
queue.offer(new Cell(m-1, j, heightMap[m-1][j]));
visited.add(0*n+j);
visited.add((m-1)*n+j);
}
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
while (!queue.isEmpty()) {
Cell cur = queue.poll();
for (int[] dir : directions) {
int row = cur.row + dir[0];
int col = cur.col + dir[1];
if (row>=0 && row<m && col>=0 && col<n && !visited.contains(row*n+col)) {
visited.add(row*n+col);
res += Math.max(0, cur.height - heightMap[row][col]);
queue.offer(new Cell(row, col, Math.max(cur.height, heightMap[row][col])));
}
}
}
return res;
}
}

Leetcode: Trapping Rain Water II的更多相关文章

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

  2. [LeetCode] Trapping Rain Water II 题解

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

  3. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

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

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

  7. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  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. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

随机推荐

  1. MemcacheQ安装及使用

    一.MemcacheQ安装记录1.安装libevent查看是否已经安装了libeventrpm -qa|grep libevent如果没有安装使用yum安装yum install libevent l ...

  2. Class Abstraction -- Object Interfaces

    <?php /* PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be in ...

  3. Bluetooth Low Energy介绍

    目录 1. 介绍 2. 协议栈 3. 实现方案 3.1 硬件实现方案 3.2 软件实现方案 1. 介绍 Bluetooth low energy,也称BLE(低功耗蓝牙),在4.0规范中提出 BLE分 ...

  4. nginx常用变量

    $args, 请求中的参数; $content_length, HTTP请求信息里的”Content-Length”; $content_type, 请求信息里的”Content-Type”; $do ...

  5. qt 4.6.2 与visual studio 2005 集成(编译方法,以及中间遇到的问题)

    不知不觉在蒂森差不多半个月了,哈哈,时间过得很快,过得很充实,近来研究QT,首先嘛,肯定要学会安装了,这最基础的不会更不用说下面的啦.闲话少说,进正题,基本的安装步骤网上多的是,但参考一个大多数情况是 ...

  6. 如何在Qt 4程序中优化布局结构(表格讲解,很清楚)

    原文地址:http://blog.csdn.net/qter_wd007/archive/2010/03/13/5377882.aspx 在迄今为止讲到每一个例子中,我们只是简单的把窗口部件放置到某个 ...

  7. C/C++ 笔试、面试题目大汇总

    1.求下面函数的返回值( 微软) int func(x) { int countx =0; while(x) { countx ++; x = x&(x-1); } return countx ...

  8. [LeetCode] Pow(x, n) (二分法)

    Implement pow(x, n). 刚开始没想到,后来看remlost的博客才写出来,代码很简练: class Solution { public: double pow(double x, i ...

  9. css中的盒子模型

    css中的盒子模型 css中的盒子模型,有两种,一种是“标准 W3C 盒子模型”,另外一种是IE盒子模型.   1.w3c盒子模型 从图中可以看出:w3c盒子模型的范围包括了:margin,borde ...

  10. iOS 集成银联支付

    下载地址:https://open.unionpay.com/upload/download/Development_kit85427986.rar 其实我找了半个小时 也不知道怎么就下载好了 这个我 ...