题目:

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!

题解:

参考:低调小一(http://blog.csdn.net/wzy_1988/article/details/17752809)的解题思路

“首先,碰到这样的题目不要慌张,挨个分析每个A[i]能trapped water的容量,然后将所有的A[i]的trapped water容量相加即可
其次,对于每个A[i]能trapped
water的容量,取决于A[i]左右两边的高度(可延展)较小值与A[i]的差值,即volume[i] = [min(left[i],
right[i]) - A[i]] * 1,这里的1是宽度,如果the width of each bar is 2,那就要乘以2了”

那么如何求A[i]的左右高度呢? 要知道,能盛多少水主要看短板。那么对每个A[i]来说,要求一个最高的左短板,再求一个最高的右短板,这两个直接最短的板子减去A[i]原有的值就是能成多少水了。

所以需要两遍遍历,一个从左到右,找最高的左短板;一个从右到左,找最高的右短板。最后记录下盛水量的总值就是最终结果了。

代码如下:

 1      public int trap(int[] A) {  
 2         if (A == null || A.length == 0)  
 3             return 0;  
 4           
 5         int i, max, total = 0;
 6         int left[] = new int[A.length];
 7         int right[] = new int[A.length];  
 8   
 9         // from left to right
         left[0] = A[0];
         max = A[0];
         for (i = 1; i < A.length; i++) {  
             left[i] = Math.max(max, A[i]);
             max = Math.max(max, A[i]);
         }  
   
         // from right to left
         right[A.length-1] = A[A.length-1];
         max = A[A.length-1];
         for (i = A.length-2; i >= 0; i--) {  
             right[i] = Math.max(max, A[i]);
             max = Math.max(max, A[i]);
         }  
   
         // trapped water (when i==0, it cannot trapped any water)
         for (i = 1; i < A.length-1; i++) {  
             int bit = Math.min(left[i], right[i]) - A[i];  
             if (bit > 0)  
                 total += bit;  
         }  
   
         return total;  
     } 

对照着代码再看原来的例子:

index:  0  1  2  3  4  5  6  7  8  9  10 11

A[index]:  0  1  0  2  1  0  1  3  2  1  2  1

left[index]: 0  1  1  2  2  2  2  3  3  3  3  3

right[index]: 3  3  3  3  3  3  3  3  2  2  2  1

min[i]: 0  1  1  2  2  2  2  3  2  2  2  1

bit[i]: -  0  1  0  1  2  1  0  0  1  0  0

那么根据上表可以算出最终结果是6。

Reference:http://blog.csdn.net/wzy_1988/article/details/17752809

Trapping Rain Water leetcode java的更多相关文章

  1. Trapping Rain Water [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...

  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: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  4. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

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

  7. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  8. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  9. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

随机推荐

  1. ThreadLocal和ThreadLocalMap源码分析

    目录 ThreadLocal和ThreadLocalMap源码分析 背景分析 定义 例子 源码分析 ThreadLocalMap源码分析 ThreadLocal源码分析 执行流程总结 源码分析总结 T ...

  2. React Native性能优化之可取消的异步操作

    前沿 在前端的项目开发中,异步操作是一个不可获取的,从用户的角度来说,异步操作所带来的体验是美妙的,但有时候也会带来一些性能隐患.比如说:有一个异步请求还没有返回结果,但是页面却关闭了,这时由于异步操 ...

  3. CSU - 2058 跳一跳

    Description 冰弦非常热衷于过气微信小游戏"跳一跳",现在给出了他每次游戏时的一些信息,请你帮他计算一下每局游戏的得分. 跳一跳的游戏规则如下: 玩家操控一个小棋子,在形 ...

  4. spring boot thymeleaf常用方式

    动态和静态区别 静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/template ...

  5. hdu 5246 乱搞

    题意:题目太长直接看链接 链接:点我 乱搞题 显然,一个人要想成功,必须大于等于最强的人的战斗力,所以我们从后往前看 这里直接拿例1解释,首先递减排个序 15,13,10,9,8 作差得2,3,1,1 ...

  6. 指针式压力表自动读数:Auto Read the Value of Manometer

    指针式压力表的自动读数,用摄像头对准压力计,然后实时自动地读取压力计的读数.视频效果如下视频所示,红色数字为识别到的指针读数.

  7. weblogic安装以及异常解决方法【转】

    转自:http://shenjc2008.iteye.com/blog/1461253 下载地址: http://www.oracle.com/technetwork/middleware/weblo ...

  8. CentOS 7编译openssl

    # 编译安装zlib库wget http://zlib.net/zlib-1.2.11.tar.gztar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./conf ...

  9. 从 n 个数字中选出 m 个不同的数字,保证这 m 个数字是等概率的

    问题如上. 这是我被面试的一个题目. 我的第一反应给出的解决的方法是.开启  n 个线程并标记序号,各个线程打印出它的序号.直到有 m 个线程被调度时,停止全部线程. 打印出的序号即是 m 个等概率出 ...

  10. How to check Ubuntu version

    Below you can find some tips on how to check Ubuntu version you are currently running. The first pla ...