407. 接雨水 II

给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。

说明:

m 和 n 都是小于110的整数。每一个单位的高度都大于 0 且小于 20000。

示例:

给出如下 3x6 的高度图:

[

[1,4,3,1,3,2],

[3,2,1,3,2,4],

[2,3,3,2,3,1]

]

返回 4。

如上图所示,这是下雨前的高度图[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] 的状态。

下雨后,雨水将会被存储在这些方块中。总的接雨水量是4。

class Solution {
private static class Cell implements Comparable<Cell> {
int row;
int col;
int height; public Cell(int row, int col, int height) {
this.row = row;
this.col = col;
this.height = height;
} @Override
public int compareTo(Cell o) {
return this.height - o.height;
}
} public static int trapRainWater(int[][] heightMap) {
if (heightMap.length <= 1 || heightMap[0].length <= 1) {// <=1行或每行<=1个元素,没法3维接水
return 0;
}
boolean[][] visited = new boolean[heightMap.length][heightMap[0].length];// 默认被初始化为false
PriorityQueue<Cell> queue = new PriorityQueue<Cell>();// 小堆
int waterTraped = 0;
// 1.初始化把最外围圈入队
for (int j = 0; j < heightMap[0].length; j++) {// 上下行
queue.add(new Cell(0, j, heightMap[0][j]));
queue.add(new Cell(heightMap.length - 1, j,
heightMap[heightMap.length - 1][j]));
visited[0][j] = true;
visited[heightMap.length - 1][j] = true;
}
for (int i = 1; i < heightMap.length - 1; i++) {// 左右列,notice顶点不要重复添加,因此...from
// 1 to length-2
queue.add(new Cell(i, 0, heightMap[i][0]));
queue.add(new Cell(i, heightMap[0].length - 1,
heightMap[i][heightMap[0].length - 1]));
visited[i][0] = true;
visited[i][heightMap[0].length - 1] = true;
}
// 2.逐点收集雨水--通过优先队列找短板
Cell lowestWall;// 最矮的墙
int row, col;
int[][] direction = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };// 下右上左四个方向
while (!queue.isEmpty()) {
lowestWall = queue.poll();
for (int i = 0; i < 4; i++) {
row = lowestWall.row + direction[i][0];
col = lowestWall.col + direction[i][1];
if (row < 0 || row > heightMap.length - 1 || col < 0
|| col > heightMap[0].length - 1
|| visited[row][col] == true) {// 越界检查+已经计算检查
continue;
}
waterTraped += Math.max(
lowestWall.height - heightMap[row][col], 0);// 当前单元格高<lowestWall高,则可以接至lowestWall.height,否则不能接水
queue.add(new Cell(row, col, Math.max(lowestWall.height,
heightMap[row][col])));// key point.加入队列成为新墙,墙高取大的
visited[row][col] = true;
}
}
return waterTraped;
}
}

Java实现 LeetCode 407 接雨水 II(二)的更多相关文章

  1. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  2. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  3. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  4. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. Java实现 LeetCode 212 单词搜索 II(二)

    212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...

  6. Java实现 LeetCode 63 不同路径 II(二)

    63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...

  7. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  8. Java实现 LeetCode 264 丑数 II(二)

    264. 丑数 II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, ...

  9. Java实现 LeetCode 229 求众数 II(二)

    229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...

随机推荐

  1. Go语言接口内部布局和方法集详解

    1. 接口值内部布局   如果用户定义的类型实现了某个接口类型声明的一组方法,那么这个用户定义的类型的值就可以赋给这个接口类型的值.这个赋值会把用户定义的类型的值存入接口类型的值.赋值完成后得到的值称 ...

  2. java1.8新特性之stream

    什么是Stream? Stream字面意思是流,在java中是指一个来自数据源的元素队列并支持聚合操作,存在于java.util包中,又或者说是能应用在一组元素上一次执行的操作序列.(stream是一 ...

  3. 超过百万的StackOverflow Flutter 问题-第二期

    老孟导读:一个月前分享的<超过百万的StackOverflow Flutter 问题-第一期>受到很多朋友的喜欢,非常感谢大家的支持,在文章末尾有第一期的链接,希望此文能对你有所帮助. N ...

  4. 以前的一些word的整理

    LAMP部署 环境:虚拟机centos7 安装apache: 命令:yum install -y httpd (在执行这个命令时,可能会遇到运行yum时出现/var/run/yun.pid已被锁定,P ...

  5. 我的linux学习日记day2

    RPM  软件包管理器 目的:降低软件安装难度原理 :将软件源代码加上一套安装规则打包到一起,用户只需要运行RPM systemctl start 服务名称 开启服务systemctl stop 服务 ...

  6. js 实时获取屏幕高度并赋值

    var that = this; var h = 1344,w=750; var orderHight1 = document.body.clientHeight; var orderWidth1 = ...

  7. Docker 集成 Jenkins Gitlab 实现 CI/CD

    首先介绍下环境部分,文章中共涉及到三台服务器,分别用 Gitlab,Jenkins,Deploy 三个名称代替,部署在内网环境,同时因为政策原因,服务器无法直接连通外网.下载 Jenkins 插件时需 ...

  8. Bootstrap组件的使用

    五.常用组件 总结: boot中事件,关注两件事 1.事件是如何触发的.自定义属性触发,触发方式是这个属性的值 2.事件触发的目标 button绑定目标 data-target="#id&q ...

  9. Spring基础之AOP

    一.AOP能解决什么问题 业务层每个service都要管理事务,在每个service中单独写事务,就会产生很多重复性的代码,而且修改事务时,需要修改源码,不利于维护.为此,把横向重复的代码,纵向抽取形 ...

  10. springboot 启动报错"No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available"

    1.问题 springboot启动报错 "D:\Program Files\Java\jdk-11\bin\java.exe" -XX:TieredStopAtLevel=1 -n ...