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!

思路:

O(n) solution. for each bar, find the max height bar on the left and right. then for this bar it can hold min(max_left, max_right) - height

对于任何一个坐标,检查其左右的最大坐标,然后相减就是容积。所以, 
1. 从左往右扫描一遍,对于每一个坐标,求取左边最大值。 
2. 从右往左扫描一遍,对于每一个坐标,求最大右值。

直方图的题还有Container With Most WaterLargest Rectangle in Histogram,还可以看看Maximal Rectangle,都很有意思

代码:

 int trap(int A[], int n) {
if(n < )
return ; vector<int> maxRs(n);
int maxR = ;
for(int i = ; i < n; i++){
if(A[i] > maxR)
maxR = A[i];
maxRs[i] = maxR;
} int totalV = ;
int maxL = ;
for(int i = n-; i >= ; i--){
if(A[i] > maxL)
maxL = A[i];
totalV += min(maxL, maxRs[i]) - A[i];
}
return totalV;
}

【题解】【直方图】【Leetcode】Trapping Rain Water的更多相关文章

  1. [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 ...

  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: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  5. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  6. Leetcode Trapping Rain Water

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

  7. [LeetCode] Trapping Rain Water 栈

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

  8. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  9. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

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

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

随机推荐

  1. 六种流行的语言---C、C++、python、Java、php、C#比较[转]

    语言大餐 回归正题,本文是六种语言连接mysql数据库的代码展示,在LZ尝试的过程中,无论是语言环境搭建.mysql依赖库的导入还是代码的风格,各种语言都各有千秋.接下来,我们就让这些语言一一登场吧. ...

  2. RAID在数据库存储上的应用-转

    随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...

  3. MySql中添加用户/删除用户

    MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束): 1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 ...

  4. JBoss JMX登录需要用户名密码的解决办法

    /opt/jboss/eap5.1.2/jboss-as/server/default/conf/props/jmx-console-users.properties 取消#admin=admin的注 ...

  5. 查看Nginx、apache、MySQL和PHP的编译参数

    1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# cat /usr/local/apache/build/config.nice3.p ...

  6. VBA读取固定文件夹中txt内容

    Sub OneTxt() '打开一个txt文件 Dim Filename As Variant, extLine&, mArr() As String Dim i%, j%, txtpath ...

  7. 选择最适合你的Linux学习方法

    我们知道Linux只是一个内核,现在的Linux操作系统底层都是用这个内核,包括Android手机,所以Linux操作系统其实是将Linux内核与应用软件做一个打包,我们称之为Linux发行版.现在比 ...

  8. js计算日期的前几天的日期

    月份0---11 var date = new Date(year,fenye_arr[0]-1,fenye_arr[1]);            miao=date.getTime(); var ...

  9. 转: ORACLE索引介绍和使用

    1.什么是索引 索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据: Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引: 索引由根节点.分支节点 ...

  10. velocity插件 veloeclipse 支持eclipse4.4

    分享主页:http://pan.baidu.com/share/home?uk=2737650112#category/type=0 http://pan.baidu.com/s/12RSAy 感谢究 ...