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.


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!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

题意:

给定一个地形,计算能存多少雨水。

思路:

1. 扫数组,找到最高柱子并以此为中心,将数组分为两半。

2. 对左半部分而言,可以想象右边一定有堵墙,只需要每次更新leftMost就能决定water大小

3. 对右半部分而言,同理。

代码:

 class Solution {
public int trap(int[] height) {
// find hightest bar
int peak_index = 0;
for(int i = 0; i < height.length; i++){
if(height[i] > height[peak_index]){
peak_index = i;
}
}
// 1. from left to peak_index
int leftMostBar = 0;
int water = 0;
for(int i = 0; i < peak_index; i++){
if (height[i] > leftMostBar){
leftMostBar = height[i];
}else{
water = water + leftMostBar - height[i];
}
} // 2. from right to peak_index
int rightMostBar = 0;
for(int i = height.length - 1; i > peak_index; i--){
if (height[i] > rightMostBar){
rightMostBar = height[i];
}else{
water = water + rightMostBar - height[i];
}
}
return water;
}
}

[leetcode]42. Trapping Rain Water雨水积水问题的更多相关文章

  1. LeetCode 42 Trapping Rain Water(积水体积)

    题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意 ...

  2. [LeetCode]42. Trapping Rain Water雨水填坑

    这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水 双指针可以解决 /* 两边指针保证,保证另外一边肯定有能挡住水的地方. 如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只 ...

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

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

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

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

  5. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  6. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

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

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

  8. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

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

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

随机推荐

  1. Discuz! X3 全新安装图文教程

    Discuz! 是腾讯旗下 Comsenz 公司推出的以社区为基础的专业建站平台,帮助网站实现一站式服务.让论坛(BBS).个人空间(SNS).门户(Portal).群组(Group).应用开放平台( ...

  2. ueditor的简单用法

    先粘贴未使用ueditor之前的代码: <body> <label for="input_content">作答区:</label> <t ...

  3. flask-appbuilder +echarts 展示数据笔记

    pip install flask-appbuilder fabmanager create-app cd newapp fabmanager create-admin fabmanager run ...

  4. 简述Ajax原理及实现步骤

    简述Ajax原理及实现步骤 1.Ajax简介 概念 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML). 现在允许浏览器与务器通信 ...

  5. Xilinx------BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用

    转载-----BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用   目前,大型设计一般推荐使用同步时序电路.同步时序电路基于时钟触发沿设计,对时钟的周期.占空比.延时和抖动提出了更高的要 ...

  6. 图灵一代接入V1

    现在官方没有一代接入了,但是还是可用,留个方法 $.ajax({ type:"post", url:"http://www.tuling123.com/openapi/a ...

  7. python 字典中 重复值去除

    tuple_r_dict = lambda _dict: dict(val[::-1] for val in _dict.items()) # Python3.x tuple_r_dict(tuple ...

  8. python 实现统计ftp服务器指定目录下文件夹数目、文件数目及所有文件大小

    本次主要为满足应用方核对上传到ftp服务器的文件是否缺漏. 主要要求:指定目录下,文件夹数目/文件数目/所有文件大小,类似Windows如下功能: 模块介绍: from ftplib import F ...

  9. iphone 开发h5 踩过的坑

    html,body{ -webkit-text-size-adjust: none; }  // 当需要在中文版chrome浏览器中显示小于12px的字体时,而且此时页面放大效果会被阻止 html,b ...

  10. Mybatis运行错误:信息: SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]

    Mybatis运行出现错误提示: 五月 23, 2018 12:07:22 上午 org.springframework.jdbc.support.SQLErrorCodesFactory <i ...