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!

注意对每根柱子能盛的水并不是自身能决定的,而是取决于其左右两边的柱子。

先记录最高的柱子maxHeight,把数组一分为二,分别计算最高柱子左右两边的盛水量。

对于最高柱子左边的部分,

  从首端开始扫描,并使用leftHeight记录已扫描部分的中得最高柱子。

  如果leftHeight > curHeight,说明当前柱子能盛水,当前柱子所盛水的容量为leftHeight-curHeight

  如果leftHeight<= curHeight,说明当前柱子不能盛水,则更新leftHeight = curHeight

对于最高柱子右边的部分,从末端开始倒序扫描,求右半部分的水,即可

class Solution {
public:
int trap(int A[], int n) {
int maxHeightIndex = ;
for(int i = ; i < n; ++ i){
if(A[i] > A[maxHeightIndex]) maxHeightIndex = i;
}
int leftHeight = ,res = ;
for(int i = ; i < maxHeightIndex;++ i){
if(leftHeight > A[i]) res+=leftHeight-A[i];
else leftHeight = A[i];
}
int rightHeight = ;
for(int i = n-; i>maxHeightIndex; --i){
if(rightHeight > A[i]) res+=rightHeight-A[i];
else rightHeight = A[i];
}
return res;
}
};

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 II 题解

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

  8. leetcode Trapping Rain Water pthon

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

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

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

随机推荐

  1. centos 软件包安装与管理

    http://rpmfind.net/linux/RPM/index.html http://pkgs.org/ https://sourceforge.net/

  2. .net core

  3. Arcgis 10.1安装

    来源http://download.csdn.net/detail/l_j_kin/7310665#comment 测试有效,存着备用,转过来不自己写了 安装desktop 打开“注册机”,功能选择a ...

  4. Java Persistence with Hibernate

    我们在Java中谈到持久化时,一般是指利用SQL在关系数据库中存储数据. ORM映射元数据,JPA支持XML和JDK 5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系, 框架据此将实体对象 ...

  5. R包igraph探究

    前段时候由于项目的原因,需要画图,然后开始接触R语言的igraph包,网上零零散散的搜罗了不少的信息,放在这边交流分享的同时也给自己留个备份吧~ 1.首先是读取文件,基本选用的都是csv文件 edge ...

  6. alpha版、beta版、rc版的意思

    很多软件在正式发布前都会发布一些预览版或者测试版,一般都叫“beta版”或者 “rc版”,特别是开源软件,甚至有“alpha版”,下面来解释一下各个版本的意思. alpha版:内部测试版.α是希腊字母 ...

  7. PHP如何获取Post请求中的Json字符串数据?

    摘自:http://dianjingjiaoyu.blog.163.com/blog/static/18347920820114194642257/ 最近用到ext与PHP交互,ext把json数据p ...

  8. sqlplus连接oracle失败分析和解决

    背景: 多台Linux服务器需要安装Oracle客户端,实现和Oracle数据库连接做业务处理. 安装完第一台后,直接将安装的目录压缩并复制到其他几台机器上,启动sqlplus连接数据库时,一直提示输 ...

  9. webview滑动事件 与内部html左右滑动事件冲突问题的解决办法

    最近在做个混合app , 用html做页面,然后通过webview嵌套在activity中,效果是这样: 开始还是比较顺利,增加了菜单退出按钮,返回键页面回退功能,页面加载显示加载图标(在app端实现 ...

  10. 兼容IE8以下浏览器input表单属性placeholder不能智能提示功能

    当前很多表单提示使用了表单属性placeholder,可这属性不兼容IE8以下的浏览器,我自己写了一个兼容处理js // 兼容IE8以下浏览器input不能智能提示功能 if(navigator.ap ...