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. java两种同步机制的实现 synchronized和reentrantlock

    java两种同步机制的实现 synchronized和reentrantlock 双11加保障过去一周,趁现在有空,写一点硬货,因为在进入阿里之后工作域的原因之前很多java知识点很少用,所以记录一下 ...

  2. maven 创建web项目的标准目录结构

      maven 创建web项目的标准目录结构 CreateTime--2018年4月18日21:05:37 Author:Marydon 1.标准目录介绍(开发目录) 2.在eclipse下,目录展示 ...

  3. 【Docker】安装并测试安装成功

    1.环境描述 Centos 7 2.安装步骤 通过命令yum install docker安装 等待下载安装-,出现下图,按y继续 继续等待-出现下图按y继续 再继续等待- 知道出现上图表示安装完毕 ...

  4. 【Oracle】存储过程之完整篇

    1.语法 create [or replace] procedure pro_name[(parameter1,parameter2,...)] is|as begin plsql_sentences ...

  5. 虚拟机和宿主机不能互ping的解决办法等

    1. 虚拟机和宿主机不能互ping的解决办法:禁用无关虚拟网卡. 2. 有时有效光驱设备为cdrom1. 3. CentOS 6.3 图形界面切换用户:System->Log Out

  6. aop:declare-parents注解

    http://www.blogjava.net/jackfrued/archive/2010/02/27/314060.html <aop:aspect> <aop:declare- ...

  7. PL/SQL配置oracle客户端,登录远程数据库配置

    本地未安装Oracle数据库,但又想使用PL/SQL连接服务器端的数据库. 1.新建NETWORK文件夹, 在该文件夹下新建ADMIN文件夹, 在该文件夹下新建tnsnames.ora文件(拷贝下面的 ...

  8. 解决Java“syntax error on token enum”问题

    本来我的问题是jsp中变量名命名和保留关键字重复了,如下图.无意中又找到下面的问题和解决方案作为笔记. 解决方法:修改变量名. ==================================== ...

  9. GoogleMapsV3-----基础地图(自定义消息提示OverlayView) (转)

    <html> <head> <title> </title> <title></title>    <style type ...

  10. 百度UEditor富文本编辑器去除过滤div等标签

    将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签. 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找到如下的代 ...