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. mysql分布式集群实现原理

    做MySQL集群,例如:利用mysql cluster ,mysql proxy,mysql replication,drdb等等 有人会问mysql集群,根分表有什么关系吗?虽然它不是实际意义上的分 ...

  2. rabbitmq学习之路-rabbitmqctl

    rabbitmqctl使用 学习rabbitmq,原理之后第一个要掌握的就是rabbitmqctl这个命令的用法了,rabbitmq的管理功能最全的就是rabbitmqctl命令了,当然还有HTTP ...

  3. 使用Generator(小黑鸟)反向生成Java项目(IDEA + Maven)

    一.生成Maven项目 二.配置pom.xml文件 通用代码 <properties> <!-- 设置项目编码编码 --> <project.build.sourceEn ...

  4. 三、django rest_framework源码之权限流程剖析

    1 绪言 上一篇中分析了认证部分的源码,认证后的下一个环节就是权限判定了.事实上,权限判定肯定要与认证联合使用才行,因为认证部分不会对请求进行禁止或者是允许,而只是根据请求中用户信息进行用户身份判断, ...

  5. Kubernetes网络模型概念

    Kubernetes网络模型 Kubernetes网络模型设计的一个基础原则是:每个Pod都拥有一个独立的IP地址,而且假定所有Pod都在一个可以直接连通的.扁平的网络空间中.所以不管它们是否运行在同 ...

  6. Alpha 冲刺报告8

    组长:吴晓晖 今天完成了哪些任务: maven和idea用的不熟啊,jar包或者war包导出来一直有问题:生气了把ide扔到服务器里去运行springboot了,卡哭了,终于可以运行了,然后debug ...

  7. 【转载】CMarkup函数说明

    1.初始化Load    导入一个XML文件到CMarkup的对象中,并对它进行解析.类似C#的Load.SetDoc  从字符串中导入XML数据,并对它解析.类似C#的LoadXml. 2.输出Sa ...

  8. 51nod 1515 明辨是非 启发式合并

    1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...

  9. js面向对象编程-高级内容

    JavaScript面向对象 一.为每个对象动态添加属性或方法 功能:返回对象类型原型的引用 prototype的使用 格式:class.prototype 场景: 比如说:A写了一个类,交给B,B在 ...

  10. java8函数式接口小例子

    // Function<T, R> -T作为输入,返回的R作为输出 Function<String,String> function = (x) -> {System.o ...