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 解题报告的更多相关文章

  1. [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 ...

  2. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. [LeetCode] 42. Trapping Rain Water 解题思路

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  4. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  5. 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 ...

  6. Leetcode Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [LeetCode] Trapping Rain Water 栈

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  9. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

随机推荐

  1. OpenERP的短信(SMS)接口

    今天测试了一下OpenERP的短信(SMS)接口. 在OpenERP的Partner界面上,WebClient的右边的工具条有个“send sms”的按钮.OpenERP中发短信用的是短信的Web接口 ...

  2. 1、redis之安装与配置

    下载安装: redis-server.exe redis服务器的daemon启动程序 redis.conf redis配置文件 redis-cli.exe redis命令行操作工具.当然,也可以用te ...

  3. vim删除文件第n行到结尾、或某段内容

    1. 编辑文件 vim myShell.sh 2. 转到文件末尾 G 3. 或者转到删除内容最后的行 :set nu #显示行号,便于确定哪行 200G #光标定到200行,表示要删除n-200行的内 ...

  4. tomcat 的配置文件 server.xml 详解

    server.xml位于$TOMCAT_HOME/conf目录下,作为整个 tomcat 服务器最核心的配置文件,server.xml的每一个元素都对应了 tomcat中的一个组件,通过对xml中元素 ...

  5. MySQL-事务隔离级别设置

    加锁研究:http://www.cnblogs.com/JohnABC/p/4377529.html 先了解下 第一类丢失更新.脏读.不可重复读.幻读.第二类丢失更新 第一类丢失更新 撤销一个事务时, ...

  6. Setting up a static IP address in Ubuntu

    sudo gedit /etc/network/interfaces Change the line iface eth0 inet dhcp to iface eth0 inet static an ...

  7. Google Map 学习过程中的代码

    <!DOCTYPE html><html> <head> <title>Simple click event</title> <met ...

  8. IntelliJ IDEA 14 拉取SVN maven 多模块项目 部署tomcat 详细图解!

    二话不说 进入主题 我们创建空项目实际上是项目空间 进入主界面 想用svn必须先启用它 选择Subversion 拉取 svn项目 你会发现这里检测不到目录 我们进入 File>Seting 里 ...

  9. Android Fragment之间传递List数据

    要说的是在两个Fragment之间传递List数据,比如有个List<User>,以及传递字符串数据,比如testId,该如何从FragmentA传递到FragmentB呢? 下面这个例子 ...

  10. JMeter ----请求数据参数设置-自动增长变量

    使用Jmeter性能测试的时候, 需要录入一些测试数据, 当这些数据要插入数据库的时候, 数据库通常会要求数据不能重复, 所以无法使用同一个数据反复进行测试, 这时候就需要在每次请求的时候使用不同的请 ...