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. chrome密码管理

    chrome://settings/passwords ------------------------------- [系统盘]:\Documents and Settings\[用户名]\Loca ...

  2. CentOS 6.2下SVN服务器的安装与配置

    安装了一下SVN服务器,本文没有与Apache整合,过程如下: 一,下载相关软件: [root@youxia201 test]# wget http://subversion.tigris.org/d ...

  3. AjaxUpload.3.5.js之ASP.NET 文件上传

    一.引入js文件 <script type="text/javascript" src="/Scripts/JQuery.min.js"></ ...

  4. js事件应用

    ---恢复内容开始--- 一.自定义滚动条 var oDiv=document.getElementById('div1'); var oParent=document.getElementById( ...

  5. Eclipse汉化问题解决

    1.删除eclipse/configuration 目录下的 org.eclipse.osgi 和org.eclipse.update 两个子目录2.重新启动 eclipse

  6. Redis系列-存储篇string主要操作函数小结

    通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...

  7. json数组,随便测试

    Pid := '1001411225514227,926792194654225'; json := SA([]); json.AsArray.Add(SO(pid)); ShowMessage( j ...

  8. CSS 水平居中

    一.水平居中:行内元素解决方案 居中元素:文字.链接以及其它行内元素(inline或inline-*类型的元素,如inline-block,inline-table,inline-flex)解决方案: ...

  9. Codeforces Round #326 (Div. 2)-Duff and Meat

    题意: Duff每天要吃ai千克肉,这天肉的价格为pi(这天可以买好多好多肉),现在给你一个数值n为Duff吃肉的天数,求出用最少的钱满足Duff的条件. 思路: 只要判断相邻两天中,今天的总花费 = ...

  10. goldengate 12c对teradata的支持

    OGG12c在2014.4.24发布了对TD的最新支持版本,软件可在此下载:https://edelivery.oracle.com.新版本特性如下: 当前支持TD 14.1的捕获和交付,仍然基于Te ...