Java实现 LeetCode 407 接雨水 II(二)
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(二)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- Java实现 LeetCode 212 单词搜索 II(二)
212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...
- Java实现 LeetCode 63 不同路径 II(二)
63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- Java实现 LeetCode 264 丑数 II(二)
264. 丑数 II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, ...
- Java实现 LeetCode 229 求众数 II(二)
229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...
随机推荐
- Leetcode_45. 跳跃游戏 II
每个位置i有一个最大跳跃距离,求最小步数从0跳到n-1. dp[i]表示从0跳到i的最少步数,显然可以转移的状态就是从i-a[i]到i-1. 因为是最小步数,考虑用优先队列优化,再考虑到状态有范围的, ...
- idea 2020 配置本地 Maven 仓库
问题: 默认Maven 仓库地址在C盘,C盘是系统盘能少放东西尽量少放. 只需要简单的两步 1.File~Settings 然后搜索 maven 如下图绿框 修改成你自己的 Maven 仓库 2.Fi ...
- 使用 pyautogui 进行跨平台的 GUI 自动化操作
有个朋友最近问我有没有推荐 GUI 桌面应用自动化的技术,我只能回答他:不好意思,这个真有,他是 pyautogui.主要有三大特征: 纯纯的 python, 源码一览无余: 跨平台,linux, w ...
- JS的数据类型判断函数、数组对象结构处理、日期转换函数,浏览器类型判断函数合集
工具地址:https://github.com/BothEyes1993/bes-jstools bes-jstools 100多个基础常用JS函数和各种数据转换处理集合大全,此工具包是在 outil ...
- CPU瞒着内存竟干出这种事
还记得我吗,我是阿Q,CPU一号车间的那个阿Q. 今天忙里偷闲,来到厂里地址翻译部门转转,负责这项工作的小黑正忙得满头大汗. 看到我的到来,小黑指着旁边的座椅示意让我坐下. 坐了好一会儿,小黑才从工位 ...
- java -> HttpServletResponse
HttpServletResponse HttpServletResponse概述 我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一 ...
- 死磕synchronized底层实现
点赞再看,养成习惯,微信搜索[三太子敖丙]第一时间阅读. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系列文章. 前言 ...
- 【雕爷学编程】Arduino动手做(60)---WS2812直条8位模块
37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的.鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为 ...
- Python--WebDriverWait+expected_conditions的一个应用
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...
- css概述五
十一.显示 1.显示方式 决定元素在网页中的表现形式(块级,行内,行内块,table) 语法: display: 取值: 1.display:block: 让元素以块级的方式显示 2.display: ...