题目:

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. 【运维实战】一次linux日志分割之路——将日志按照每小时进行分割,并按照“日期-小时”格式保存

    是这样的,现在需要对nginx的access.log进行按照每小时进行分割,并且最好还要能够以 “日期+时间”的形式,命名保存. 两点,一个是按照每小时进行分割,一个是将日志以“日期+时间”的形式进行 ...

  2. 命令:which、whereis、who和w

    开始 命令搜索的顺序 在shell function中查找,有则调用,无则下一步: 判断命令是否为bash内置命令,有则调用,无则下一步: 在$PATH中搜索该命令,有则调用,无则报错. 判断命令类型 ...

  3. RMQ_第一弹_Sparse Table

    title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...

  4. 第1天:Django框架简介与工程创建

    Django简介 Django特点 环境搭建 创建工程 启动服务 使用pycharm打开工程 创建子应用 Django简介 Django,是用Python语言写的开源web开发框架,并遵循MVC设计. ...

  5. [leetcode sort]75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  6. 【SPFA】POJ1860-Currency Exchange

    [题目大意] 给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换,能否赚. [思路] 反向运用SPFA,判断是否有正环.每次队首元素出队之后,判断一下到源点s的距离是否增大,增大则返回tr ...

  7. Kibana安装及使用说明

    Kibana安装及使用说明 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在Elasticsearch索引中的数据. 官方地址:https://www. ...

  8. .NET面试宝典-基础

    经典.net面试题目 建议使用朗读女软件下载,边读边记. 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有 ...

  9. MySQL从库com_insert无变化的原因

    大家都知道com_insert等com_xxx参数可以用来监控数据库实例的访问量,也就是我们常说的QPS.并且基于MySQL的复制原理,所有主库执行的操作都会在从库重放一遍保证数据一致,那么主库的co ...

  10. 重温PHP面向对象的三大特性

    PHP面向对象的三大特性:封装性.继承性.多态性. 1. 封装性: 也称为信息隐藏,就是将一个类的使用和实现分开,只保留部分接口和方法与外部联系,或者说只公开了一些供开发人员使用的方法. 于是开发人员 ...