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. 今天主要做的是Remember Me(记住我)功能的实现

    功能就是让网站登录过的人只要不注销,下次打开网站之后直接进入,不用重复登录,此功能主要是session与cookie的配合运用,具体实现是这样的,在登录页面判断并完成登录,然后将所需数据写入sessi ...

  2. Python:日薪工资计算

    劳动者离职,当天要结清工资,实际操作是当天算清,三日内结清.有的公司省人力和吃利息,统一计算,统一下月月底发放. 有时要验算下离职工资,用Python操作一番,输入计时天数.请假小时.加班小时.基本工 ...

  3. Web_php_unserialize-攻防世界XCTF

    0x00 简介 记录一下,重点是记录一下那篇正则文章. 0x01 题目代码 <?php class Demo { private $file = 'index.php'; public func ...

  4. 【数论基础】素数判定和Miller Rabin算法

    判断正整数p是否是素数 方法一 朴素的判定   

  5. JDK 安装与环境变量配置

    JDK官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.下载jd ...

  6. ASHRAE KAGGLE大能源预测(前三名方案总结+相关知识点讲解+python实现)

    @ 目录 1 概述 2 处理思想学习 2.1 移除异常值 2.2 缺失值 2.3 目标函数 2.4 特征工程 2.4.1 Savitzky-Golay filter 2.4.2 Bayesian ta ...

  7. ORA-12519,TNS:no appropriate service handler found的问题 超过连接数

    http://www.2cto.com/database/201205/133542.html ORA-12519,TNS:no appropriate service handler found的问 ...

  8. Havel定理 poj1659

    http://blog.csdn.net/xcszbdnl/article/details/14174669 代码风格这里的 Frogs' Neighborhood Time Limit: 5000M ...

  9. CF1340B Nastya and Scoreboard(暴搜剪枝/dp)

    Question 一个n个数码位的分数板,每一个数码位都是一个七段数码管,现在给出每个数码位的显示情况,问再点亮k段数码管的话能显示的最大的数是多少,如果不能构成一串数字,就输出-1 Solution ...

  10. 写给程序员的机器学习入门 (五) - 递归模型 RNN,LSTM 与 GRU

    递归模型的应用场景 在前面的文章中我们看到的多层线性模型能处理的输入数量是固定的,如果一个模型能接收两个输入那么你就不能给它传一个或者三个.而有时候我们需要根据数量不一定的输入来预测输出,例如文本就是 ...