Trapping Rain Water I && II
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Analysis:
We first find out the max height in the array, then we start from the leftmost bar which is considered as the wall of the container. If there is a bar whose height is less than the wall, water will be saved above that bar. We do the same operation from rightmost to the highest bar position.
public class Solution {
public int trap(int[] height) {
if (height == null || height.length <= ) return ;
int maxIndex = ;
for (int i = ; i < height.length; i++) {
if (height[i] > height[maxIndex]) {
maxIndex = i;
}
}
int leftMax = height[];
int total = ;
for (int i = ; i < maxIndex; i++) {
if (height[i] < leftMax) {
total += (leftMax - height[i]);
} else {
leftMax = height[i];
}
}
int rightMax = height[height.length - ];
for (int i = height.length - ; i > maxIndex; i--) {
if (height[i] < rightMax) {
total += (rightMax - height[i]);
} else {
rightMax = height[i];
}
}
return total;
}
}
Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.
Note:
Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000.
Example:
Given the following 3x6 height map:
[
[1,4,3,1,3,2],
[3,2,1,3,2,4],
[2,3,3,2,3,1]
] Return 4.

The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.

After the rain, water is trapped between the blocks. The total volume of water trapped is 4.
分析:
从四周出发,选取最低点(木桶原理),然后选取周围没有被visited的点。找到更低的点,则把当前点和低点的差值作为可以装水的量,注意,在加入新的点的时候,那个点的高度应该使用当前点的高度,这样我们就不用倒着回去找最高点了。
class Solution {
public int trapRainWater(int[][] heights) {
if (heights == null || heights.length == || heights[].length == ) return ;
PriorityQueue<Cell> queue = new PriorityQueue<>(, (cell1, cell2) -> cell1.height - cell2.height);
int row = heights.length, col = heights[].length;
boolean[][] visited = new boolean[row][col];
// add border cells to the queue.
for (int i = ; i < row; i++) {
visited[i][] = true;
visited[i][col - ] = true;
queue.offer(new Cell(i, , heights[i][]));
queue.offer(new Cell(i, col - , heights[i][col - ]));
}
for (int i = ; i < col; i++) {
visited[][i] = true;
visited[row - ][i] = true;
queue.offer(new Cell(, i, heights[][i]));
queue.offer(new Cell(row - , i, heights[row - ][i]));
}
// from the borders, pick the shortest cell visited and check its neighbors:
// if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped
// add all its neighbors to the queue.
int[][] dirs = new int[][]{{-, }, {, }, {, -}, {, }};
int res = ;
while (!queue.isEmpty()) {
Cell cell = queue.poll();
for (int[] dir : dirs) {
int neighbor_row = cell.row + dir[];
int neighbor_col = cell.col + dir[];
if (neighbor_row >= && neighbor_row < row && neighbor_col >= && neighbor_col < col && !visited[neighbor_row][neighbor_col]) {
visited[neighbor_row][neighbor_col] = true;
res += Math.max(, cell.height - heights[neighbor_row][neighbor_col]);
queue.offer(new Cell(neighbor_row, neighbor_col, Math.max(heights[neighbor_row][neighbor_col], cell.height)));
}
}
}
return res;
}
}
class Cell {
int row;
int col;
int height;
public Cell(int row, int col, int height) {
this.row = row;
this.col = col;
this.height = height;
}
}
Trapping Rain Water I && II的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
随机推荐
- php内置函数分析之array_diff_assoc()
static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_type) /* {{{ */ { uint ...
- 使用idea对XML的增删改查
XML:是一种可扩展标记性的语言,与java语言无关,它可以自定义标签. 1.首先需要到导入Dom4j架包,与自己所时候的ide关联 2.编写自己的xml文件,入上图所示(里面的所有元素及元素中的属性 ...
- Linux学习-FTP服务
一.FTP相关介绍 1.文本传输协议FTP FTP (File Transfer Protocol) 文件传输协议,是因特网中使用最广泛的文件传输协议: 基于C/S结构的双通道协议(数据和命令连接) ...
- 放一道比较基础的LCA 的题目把 :CODEVS 2370 小机房的树
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...
- 基于点云的3ds Max快速精细三维建模方法及系统的制作方法 插件开发
基于点云的3ds Max快速精细三维建模方法及系统的制作方法[技术领域][0001]本发明涉及数字城市三维建模领域,尤其涉及一种基于点云的3d ...
- VMware 15 安装 macOS 10.14优质教程链接集合
https://www.jianshu.com/p/25d2d781bd98 https://mp.weixin.qq.com/s/91Qc7L7E0xbVYXUcReUb_w https://blo ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 10.可编程对象
Chapter 10 Programmable Objects 声明和赋值一个变量: DECLARE @i AS INT; SET @i = 10; 变量可以让你暂时存一个值进去,然后之后再用,作用域 ...
- ProtocolHandler继承体系
- Oracle Flashback Database
Oracle Flashback Database Ensure that the prerequisites described in Prerequisites of Flashback Data ...
- pycharm中添加python3 的环境变量
i卡是HDKJHA{{sadfsdafdsafd.jpg(uploading...)}}S{{53ad37a938001.jpg(uploading...)}}