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. docker lamp

    可以直接使用官方镜像搭建LAMP环境从官方下载PHP+Apache镜像和MySQL两个镜像来组成(如:php:7.2.3-apache-stretch和mysql:5.7.21)docker pull ...

  2. websocket 11

    1. websocket 回顾: - 什么是轮训? - 通过定时器让程序每隔n秒执行一次操作. - 什么是长轮训? - 浏览器向后端发起请求,后端会将请求 hang 住,最多hang 30s. 如果一 ...

  3. goaccess 通过jsonpath 转换为prometheus metrics

    goaccess 是一个不错的日志分析工具,包含了json 数据同时支持基于websocket 的实时数据处理,当然我们可以通过jsonpath 的exporter 转换为支持promethues 的 ...

  4. Game Physics Cookbook (Gabor Szauer 著)

    Chapter1: Vectors Chapter2: Matrices Chapter3: Matrix Transformations Chapter4: 2D Primitive Shapes ...

  5. (翻译).NET应用架构

    .NET应用架构 Kalyan Bandarupalli著,hystar翻译 这个系列文章将帮助.NET开发人员与架构师使用最新的.NET技术设计高效的.NET应用.关于应用架构这方面虽然已有很多文章 ...

  6. docker安装solr集群5.3.1

    docker-compose.yml: version: '3' services: zookeeper-A: image: zookeeper:3.4.11 ports: - "12181 ...

  7. 承接AR定制AR项目外包(正规公司,内附案例)

    京团队长年承接AR项目外包 咨询QQ:372900288  微信:liuxiang0884 以下是AR项目案例演示,索取更多案例请通过以上方式在线联系我们

  8. LiveBindings --- 把对象之间的属性绑定起来

    有了 FireMonkey 框架,它不同于 VCL ,以往的数据感知控件不能放在它上面,所以 XE2 提供了 LiveBindings 功能作为替代方案.另外它也是个通用的基础设施,同样可用于传统的V ...

  9. CentOS 7安装WordPress

    在开始本文前,我假定你已经安装好了nginx.php-fpm和mariaDB(或mysql).它们的安装过程可参考我以前的文章. 1. 安装EPEL(Extra Packages for Enterp ...

  10. Windows系统中监控文件复制操作的几种方式

    http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...