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. ubuntu 14.04 Bob 安装

    1. 附件依赖项安装$ sudo add-apt-repository ppa:biometrics/bob $ sudo apt-get update $ sudo apt-get install ...

  2. 牛客练习赛1 B - 树

    链接:https://www.nowcoder.com/acm/contest/2/B来源:牛客网 题目描述 shy有一颗树,树有n个结点.有k种不同颜色的染料给树染色.一个染色方案是合法的,当且仅当 ...

  3. map赋值前要先初始化:assignment to entry in nil map

    注意这种map的嵌套的形式,make只初始化了map[string]T部分(T为map[int]int),所以下面的赋值会出现错误: test := make(map[string]map[int]i ...

  4. 深入剖析cpp对象模型

    C++对象模型可以概括为以下2部分: 1. 语言中直接支持面向对象程序设计的部分,主要涉及如构造函数.析构函数.虚函数.继承(单继承.多继承.虚继承).多态等等. 2. 对于各种支持的底层实现机制.在 ...

  5. Centos 安装 WPS

    Linux有自己的一套类是Office的办公软件:LibreOffice,但是不是很友好,幸好WPS有Linux版本. 安装步骤: 1.安装依赖包 yum install libpng12 yum i ...

  6. BZOJ.3105.[CQOI2013]新Nim游戏(线性基 贪心 博弈论)

    题目链接 如果后手想要胜利,那么在后手第一次取完石子后 可以使石子数异或和为0.那所有数异或和为0的线性基长啥样呢,不知道.. 往前想,后手可以取走某些石子使得剩下石子异或和为0,那不就是存在异或和为 ...

  7. 中国气象局某分院官网漏洞打包(弱口令+SQL注入+padding oracle)

    漏洞一.后台弱口令 后台地址:http://www.hnmatc.org/admin/ 直接爆破得到账号admin  密码admin888 漏洞二.SQL注入(前台后台都有) 注入点:http://w ...

  8. Spring Boot 运作原理

    Spring Boot 运作原理 1.Spring Boot 简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了 ...

  9. 认识javascript中的作用域和上下文

    javascript中的作用域(scope)和上下文(context)是这门语言的独到之处,这部分归功于他们带来的灵活性.每个函数有不同的变量上下文和作用域.这些概念是javascript中一些强大的 ...

  10. Python168的学习笔记5

    关于对csv文件的操作. python标准库中有csv的库,使用非常方便. import csv with open('pingan.csv','rb') as rf: reader = csv.re ...