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

首先找到最高的bar,然后从左到这个最高的bar,依次计算面积;再从右到这个最高的bar,依次计算面积。时间复杂度n*2 = O(n)

class Solution {
public int trap(int[] height) {
int ret = 0;
int curIndex = 0; //index of highest
int topIndex = 0;
for(int i = 1; i < height.length; i++){
if(height[i] > height[topIndex]) topIndex = i;
} for(int i = 0; i < topIndex; i++){
if(height[i] > height[curIndex]){
curIndex = i;
}
else{
ret += (height[curIndex]-height[i]);
}
} curIndex = height.length-1;
for(int i = height.length-2; i > topIndex; i--){
if(height[i] > height[curIndex]){
curIndex = i;
}
else{
ret += (height[curIndex]-height[i]);
}
} return ret;
}
}

42. Trapping Rain Water (JAVA)的更多相关文章

  1. LeetCode - 42. Trapping Rain Water

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

  2. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  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 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. 刷题42. Trapping Rain Water

    一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...

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

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

  9. 【LeetCode】42. Trapping Rain Water

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

随机推荐

  1. vue 使用 axios 时 post 请求方法传参无法发送至后台

    axios 时 post 请求方法传参无法发送至后台报错如下 Response to preflight request doesn't pass access control check: No ' ...

  2. REST framework 之 分页

    DRF分页组件 为什么要使用分页 我们数据表中可能会有成千上万条数据,当我们访问某张表的所有数据时,我们不太可能需要一次把所有的数据都展示出来,因为数据量很大,对服务端的内存压力比较大还有就是网络传输 ...

  3. Spring Boot 访问到页面返回数据乱码

    在@RequestMapping注解中增加produces="application/json;charset=UTF-8"即可 例如:@RequestMapping(value ...

  4. 一句话搞定python六剑客

    六剑客 一行搞定六剑客:三个函数:map filter reduce + lambda 切片 推导列表 python最有特点的一行代码,所有代码均可以借用一行代码(目标) 1.map(函数,列表或者字 ...

  5. nginx回源使用localhost产生问题

    最近测试ngx_http_slice模块,回源的时候填的localhost结果老是超时,还以为是slice模块有问题,后来无意间改成127.0.0.1后就没有问题了 真是见鬼了 #user root; ...

  6. python-接口开发flask模块(二)全局host配置

    设置全局变量优势很多主要是可以方便修改参数不需要每个代码单独修改,只修改host配置就可以,减少出错率,提高工作效率MYSQL_HOST = 'XXX.XXX.CCC.XXX' MYSQL_PORT ...

  7. C# 创建和引入动态链接库dll文件

    一.创建动态链接库dll文件 新建 -> 项目->类库 名称为:dlltest 添加函数:消息框弹出消息 using System.Collections.Generic; using S ...

  8. delphi 每英寸相素点取值偏差

    在所有资料中,每英寸相素点之比一般是这两个值,即:0.0393700788  25.399999961392 但是在GDI编程中,却遇到LOGPIXELSX  LOGPIXELSY 在取值为96DPI ...

  9. Struts2测试题

    今天给大家看一套我最近做的一套关于Struts2的题: 1.以下关于jQuery说法错误的选项是( D ). A.“$”为jQuery脚本库的默认全局变量名,即“$” = “jQuery” B.$.a ...

  10. 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_3 Mybatis中编写dao实现类的使用-修改删除等其他操作

    update和上面的Insert代码基本是一样的,只需要修改这里, 测试Update的方法 删除 findById 测试方法 findByName 测试方法 findTotal