题目:

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. 五、django rest_framework源码之版本控制剖析

    1 绪论 Djangorest_framework的版本控制允许用户更改不同客户端之间的行为,且提供了许多不同的版本控制方案.版本控制由传入的客户端请求确定,可以基于请求URL,也可以基于请求标头. ...

  2. 美团在O2O场景下的广告营销

    美团作为中国最大的在线本地生活服务平台,覆盖了餐饮.酒店.旅行.休闲娱乐.外卖配送等方方面面生活场景,连接了数亿用户和数百万商户.如何帮助本地商户开展在线营销,使得他们能快速有效地触达目标用户群体提升 ...

  3. 把eclipse写好的web项目导入idea 部署到Tomcat

    主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此例中选 ...

  4. 转 Java高级程序员面试题

    1.你认为项目中最重要的过程是那些? 分析.设计阶段  尽量找出进度的优先级 2.如果给你一个4-6人的team,怎么分配? 挑选一技术过硬的人作为我的替补.其它人平均分配任务,每周进行全面的任务分配 ...

  5. Openstack-开发基础 stevedore学习

    在给openstack-N版加路由的时候发现怎么都无法搞定,原来现在用这个模块来处理了 stevedore是用来实现动态加载代码的开源模块.它是在OpenStack中用来加载插件的公共模块.可以独立于 ...

  6. hdu 2216 bfs

    题目大意:两个东西朝相同方向移动Sample Input4 4XXXX.Z...XS.XXXX4 4XXXX.Z...X.SXXXX4 4XXXX.ZX..XS.XXXXSample Output11 ...

  7. MySQL数据库基准压力测试工具之MySQLSlap使用实例

    一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...

  8. oracle开发学习篇之集合运算符以及集合异常捕获

    --取出集合;长度 declare type list_nested ) not null; v_all list_nested := list_nested('a','b','c','d','c', ...

  9. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  10. Quartz实现动态定时任务

    一. 说明 由于最近工作要实现定时任务的执行,而且要求定时周期是不固定的,所以就用到了quartz来实现这个功能: spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持q ...