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 ...
随机推荐
- UVa 1303 - Wall
题目:有非常多点.修一座最短的围墙把素有点围起来,使得全部点到墙的距离不小于l. 分析:计算几何,凸包. 假设.没有距离l的限制.则答案就是凸包的周长了.有了距离限制事实上是添加了2*π*l. 证明: ...
- java String字符串
五.java数据类型之String(字符串) CreateTime--2017年7月21日16:17:45 Author:Marydon (一)数据格式 (二)初始化 // 方式一 String ...
- oracle 插入表数据的4种方式
1.往表中插入一整行数据 /*方法一*/ INSERT INTO 表名 VALUES(val1,val2,val3,...); /*方法二*/ '; 如: ,, FROM DUAL; 注意: 2. ...
- JDBC实例--通过连接工具类DBUtil +配置文件来获取连接数据库,方便又快捷
根据前面的连接方法,还有缺点就是,如果人家要换数据库,还得改动源代码,然后还要编译什么的.这样客户修改也不容易. 做法:我们写一个配置文件,把该数据写在配置文件上面,然后通过类来加载改文件,然后读取相 ...
- 从一个Idea到产品需要经历哪些阶段?
从一个Idea到产品需要经历哪些阶段? Lkey 07月19日 16:520 现实工作中,不免遇到这样的情况.什么嘛?老板(领导)又有新想法了?又有其他Idea了?心里一阵骂娘xxxxxx.或者产品负 ...
- PmException--- SQL(统计报表)
select TAGS,GROUP_CONCAT(TAGS) t from EXCEPTION_RESULT e,PM_TASK t ' and t.OWNER='admin' group by TA ...
- PHP-PHP.INI常用配置详解
variables_order: 假如为'GPCS'表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER, 此时$_ENV为空数组, 只要把'E'添加到'GPCS'后 ...
- windows系统定时重启自定义exe程序
工作需要, Windows系统定时重启自定义exe程序. 写了如下程序, 按照说明(readme.txt)修改批处理文件中的四个参数即可: 1.readme.txt 第一个参数:进程名(不用带exe) ...
- 初学HTML一些基本控件语句
<html> <head> <title> 这是网页的标题</title> </head> <body> <h2>& ...
- Eclipse折叠代码 coffee bytes code folding
提供一个插件下载地址,博客园的: http://files.cnblogs.com/wucg/com.cb.eclipse.folding_1.0.6.jar.zip 将下载的zip文件解压出来的j ...