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. 用我所学去讲C语言指针

    文章更新,更加详细的介绍请看这篇:https://www.cnblogs.com/lulipro/p/7460206.html 很多人不敢讲C的指针,有些人讲不清,有些人怕讲错.初生牛犊不怕虎,就让我 ...

  2. addEventListener以及滑轮滑动事件的应用

    addEventListener用于向元素添加事件,而其适用于较新版的IE浏览器(如IE9),对于IE6/7/8来说,应该用attachEvent 下面的代码即为向<img>元素添加事件 ...

  3. node 下less无法编译的问题

    vue+less的项目中,npm run dev不通过,提示以下错误: These dependencies were not found: * !!vue-style-loader!css-load ...

  4. 使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)

    使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development.production) --mode 指定环境模式 ...

  5. 验证代理ip是否可用

    改编自:http://www.jianshu.com/p/588241a313e7 # _*_ coding:utf-8 _*_ import urllib2 import re class Test ...

  6. enctype=“multipart/form-data”详解

    enctype这个属性管理的是表单的MIME(Multipurpose Internet Mail Extensions)编码,共有三个值可选: 1.application/x-www-form-ur ...

  7. background-size的应用情景:当给出的背景图片大于实际网页需要布局的图片大小时

    网页需求是:50*50 如果只设置  width:50px;height:50px;background-image("images/XXX.png"); 效果如下: 添加设置:b ...

  8. k8s学习笔记之五:Pod资源清单spec字段常用字段及含义

    第一章.前言 在上一篇博客中,我们大致简述了一般情况下资源清单的格式,以及如何获得清单配置的命令帮助,下面我们再讲解下清单中spec字段中比较常见的字段及其含义 第二章.常用字段讲解 spec.con ...

  9. halcon批量读取图片

    以前这个代码都是自己写,不仅繁琐,而且容易忘记.其实Halcon中提供了相关的方法.记录一下吧,其实很简单. 读取一个文件夹下的所有图片[助手]>[打开新的image acquisition ] ...

  10. 使用jQuery+huandlebars判断类型的helper

    兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...