Leetcode Trapping Rain Water
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的更多相关文章
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- 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 ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
随机推荐
- JVM内存模型、指令重排、内存屏障概念解析
在高并发模型中,无是面对物理机SMP系统模型,还是面对像JVM的虚拟机多线程并发内存模型,指令重排(编译器.运行时)和内存屏障都是非常重要的概念,因此,搞清楚这些概念和原理很重要.否则,你很难搞清楚哪 ...
- springmvc @PathVariable("b") double b 丢失精度问题
Spring MVC从3.0开始支持REST,而主要就是通过@PathVariable来处理请求参数和路径的映射.由于考虑到SEO的缘故,很多人喜欢把新闻的名称作为路径中的一部分去处理,这时候中文的名 ...
- Python之路【第二十一篇】Django ORM详解
ORM回顾 关系对象映射(Object Relational Mapping,简称ORM). django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表. 对于ORM框 ...
- Sql Server 分区演练 【转】
Sql Server 分区演练 [转] 代码加注释,希望对初学者有用. USE [master]GOif exists (select * from sys.databases where name ...
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
功能说明 输入文件路径,在浏览器输出文件预览信息,经测试极速(Chrome).IE9.Firefox通过 分类文件及代码说明 DemoFiles 存放可测试文件 Default.aspx 启动页 ...
- yaf设置命名空间
修改yaf配置文件 文件是:yaf.ini extension=yaf.so yaf.use_namespace=1 index文件. 目录是application/controllers/Index ...
- IoC/DI基本思想的演变
---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...
- 万恶的jar包冲突
搭了个spring+struts2+mybatis的项目架子, 好久不用myEclipse和tomcat了,生疏了好多. 建议还是去百度一些框架整合的博客,直接使用博客里面给的jar包列表里的jar包 ...
- iOS为真机调试增加scribble来定位野指针
尽管在ARC中,野指针出现的频率已经大大降低了,但是仍然会有野指针困扰着我们. 在模拟器调试中,我们可以开启scribble或者zombieObject来将已经释放的内存填充无意义的内容,能够将一些非 ...
- cookie中文乱码
在学习当中碰到cookie中文乱码问题,问题原因:cookie对中文不太支持,将中文放入cookie中会报错误. 解决办法: 1.编码 将中文进行编码再放入cookie中: String userna ...