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 ...
随机推荐
- [Unity3D]添加音效说明
添加音效组件并添加音乐资源 其中Pitch用来提高和降低音调的,比如可以和赛车游戏的轮胎绑定,当轮胎越快,则音调越高 2D/3D音效:2D音效和摄影家的距离无关,可以看做是一个背景音乐:而3D音效则是 ...
- linux rpm安装apache php mysql
CentOS 可以通过 yum 安装: yum -y install httpd php php-mysql mysql-serverservice httpd status|start|stop| ...
- C#学习笔记
1.C#中[],List,Array,ArrayList的区别 [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList ...
- PHP判断文件或者目录是否可写
在PHP中,可用is_writable()函数来判断一个 文件/目录 是否可写,详情如下: 参考 is_writable (PHP 4, PHP 5) is_writable — 判断给定的文件名是否 ...
- Maven No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...
- php 操作数组 (合并,拆分,追加,查找,删除等)
1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...
- PHP PHPUnit的简单使用
1.window安装pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 2.在工作目录下,放两个文件: 1)C ...
- Angular2 入门
1. 说明 该文档为Angular2的入门文档,可以根据该文档的内如做出一个“helloworld”类型的Angualr2入门程序,通过该文档可以初步了解Angular2的相关知识以及开发流程,同时搭 ...
- MVC 前台获取三级菜单及子菜单
1.在后台将所有的菜单获取出来,代码如下: public ActionResult Index() { //所有商品分类 var oneMenu = _baseGoodsCategory.FindLi ...
- SpringMVC基本配置
1 springmvc 框架 1.1 回顾mvc结构 1.2springmvc的介绍 是一个基于mvc的web层框架,使用spring的一个子项目 在使用springmvc的时候,不用单独和spri ...