LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/
Trapping Rain Water
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.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
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!
SOLUTION 1:
从左到右扫描,计算到从左边到curr的最高的bar,从右到左扫描,计算到从右边到curr的最高的bar。
再扫描一次,把这两者的低者作为{桶}的高度,如果这个桶高于A[i]的bar,那么A[i]这个bar上头可以存储height - A[i]这么多水。把这所有的水加起来即可。
public class Solution {
public int trap(int[] A) {
if (A == null) {
return ;
}
int max = ;
int len = A.length;
int[] left = new int[len];
int[] right = new int[len];
// count the highest bar from the left to the current.
for (int i = ; i < len; i++) {
left[i] = i == ? A[i]: Math.max(left[i - ], A[i]);
}
// count the highest bar from right to current.
for (int i = len - ; i >= ; i--) {
right[i] = i == len - ? A[i]: Math.max(right[i + ], A[i]);
}
// count the largest water which can contain.
for (int i = ; i < len; i++) {
int height = Math.min(right[i], left[i]);
if (height > A[i]) {
max += height - A[i];
}
}
return max;
}
}
2015.1.14 redo:
合并2个for 循环,简化计算。
public class Solution {
public int trap(int[] A) {
// 2:37
if (A == null) {
return ;
}
int len = A.length;
int[] l = new int[len];
int[] r = new int[len];
for (int i = ; i < len; i++) {
if (i == ) {
l[i] = A[i];
} else {
l[i] = Math.max(l[i - ], A[i]);
}
}
int water = ;
for (int i = len - ; i >= ; i--) {
if (i == len - ) {
r[i] = A[i];
} else {
// but: use Math, not max
r[i] = Math.max(r[i + ], A[i]);
}
water += Math.min(l[i], r[i]) - A[i];
}
return water;
}
}
CODE:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/Trap.java
LeetCode: Trapping Rain Water 解题报告的更多相关文章
- [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] 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 ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- 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 Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
随机推荐
- 整理两个JVM博客集合,空闲时候可以看
纯洁的微笑写的:https://www.cnblogs.com/ityouknow/p/5614961.html 集合:http://www.cnblogs.com/ityouknow/categor ...
- 基于FFmpeg的音频编码(PCM数据编码成AAC android)
概述 在Android上实现录音,并利用 FFmpeg将PCM数据编码成AAC. 详细 代码下载:http://www.demodashi.com/demo/10512.html 之前做的一个demo ...
- Spring MVC Beginner’s Guide勘误表
- 17 submitted: last submission 09 Dec 2016 Page number: 213 Qauntity should be: Quantity Page numbe ...
- HDUOJ----旋转的二进制
旋转的二进制 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDUOJ--4565 So Easy!
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDUOJ---大菲波数
大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Java获取资源的路径
在Java中,有两种路径: 类路径 文件夹路径 使用类路径有两种方式: object.getClass().getResource()返回资源的URL MyClass.class.getResourc ...
- python中如何对list之间求交集,并集和差集
最近遇到一个从list a里面去除list b的元素的问题,由于a很大,b也不小.所以遇到点困难,现在mark一下. 先说最简单的方法: a = [1, 2, 3, 4, 5, 6, 7, 8, 9, ...
- 解决ADSL拨号上网错误691:由于域上的用户名和密码无效而拒绝访问
此错误是发生在我家用一个台式机拨号上网没问题,但笔记本拨号上网就有问题. 问题解决发现是电信初次拨号上网会绑定这个拨号用户的MAC网卡地址,将台式机的MAC地址配置到我的笔记本上就ok了! ...
- Android应用的自动升级、更新模块的实现
我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Android系统的软件包管理和安装机制,这一功能实现起来相当简单,下面我们就来实践一下.首先给出界面效果: ...