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. 浅析Spring中bean的作用域

    一.前言   刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...

  2. JavaWeb实战:报价计算系统(layui+tomcat+cookie实现)

    JavaWeb实战:报价计算系统(layui+tomcat+cookie实现) 系统概述: 该系统是文物物流公司的一个小功能模块,用于帮助用户计算运费.点击查看实际效果 系统文档: 添加展品: 在表单 ...

  3. 学习python的第一天,python的简单知识

    python 是现如今比较火的一种编程语言.在抱着试试的态度我来进行学习下python.要学习python 要先进行环境的安装. 下面是下载链接:https://pan.baidu.com/s/1PW ...

  4. .NET Core接入ElasticSearch 7.5

    写在前面 最近一段时间,团队在升级ElasticSearch(以下简称ES),从ES 2.2升级到ES 7.5.也是这段时间,我从零开始,逐步的了解了ES,中间也踩了不少坑,所以特地梳理和总结一下相关 ...

  5. JUC(3)---CountDownLatch、CyclicBarrier和AQS

    CountDownLatch可以让一个线程等待其他线程完成了各自的工作之后再执行.比如说一个切菜,一个人切肉,都准备完毕之后才能炒肉. 构造方法: public CountDownLatch(int ...

  6. Gradle 多环境、多渠道打包

    最近项目从eclipse迁移到AS上,主要是为了使用gradle这个插件进行apk打包.毕竟程序员还是要与时俱进的.首先介绍下项目主要需求: 1.使用百度统计,需要对个平台的信息进行统计 2..api ...

  7. mysql+redis缓存策略常见的错误

    什么时候应该更新缓存 应该是从数据库读取数据后,再更新缓存,从缓存读取到数据,就不需要再重新写缓存了,一个常见的错误是,每次访问接口都更新缓存,这样的话,如果接口一直有流量,那么db中的数据,就一直没 ...

  8. AJAX一

    一.http协议 1.URL http://www.baidu.com/md/index.html 结构:协议+主机名称+目录结构+文件名称 URL完整的结构 <scheme>://< ...

  9. 02.drf不使用serializers返回数据

    drf 可以使用不经过model和serialzier的数据返回,也可以配置权限 class DashboardStatusViewset(viewsets.ViewSet): "" ...

  10. ftp服务器搭建(二)

    1.已经安装好了vsftpd  进入到根目录下的/etc目录 ls查看一下 2.拷贝一下上面的两个配置文件 我拷贝到了我新建的目录中了 3.查看现在的网络连接方式——我的是-net方式 当然其他方式也 ...