[LintCode] 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.
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
O(n) time and O(1) memory
O(n) time and O(n) memory is also acceptable.
class Solution {
public:
int trap(vector<int>& heights) {
if(heights.size() < ) return ;
int waterNum = , left = , right = heights.size() - ,
leftH = heights[left], rightH = heights[right]; while(left < right){
if(leftH < rightH){
int t = left + ;
if(t < right){
if(heights[t] < leftH)
waterNum += leftH - heights[t];
else leftH = heights[t];
}
left = t;
}else{
int t = right - ;
if(t > left){
if(heights[t] < rightH)
waterNum += rightH - heights[t];
else rightH = heights[t];
}
right = t;
}
}
return waterNum;
}
};
[LintCode] Trapping Rain Water的更多相关文章
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping rain water II
Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- Integer Inquiry
Integer Inquiry Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Sub ...
- A+B Again(在某个数中找大于m的最小约数)
A+B Again Accepted : 15 Submit : 243 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描述 上次趣味赛小明的 ...
- 习惯mac . OS X 安装MacPorts
1. 安装必要的软件 安装MacPorts需要先安装X11和Xcode,Lion系统已经默认安装好了X11,在Appstore里安装Xcode即可. 下载完Xcode之后,一般路径在/Applicat ...
- HDU 1062 Text Reverse(水题,字符串处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...
- NGUI无限滑动
http://www.unity蛮牛.com/blog-9383-1391.html 最近由于工作需要,就开始研究NGUI滑动.刚开始参考NGUI自带的循环滑动,利用隐藏和显示,提高GPU的渲染,但是 ...
- 在link的url里新增参数
(文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) <%= link_to image_tag("/images/icons/aaa. ...
- C#开发实例 键盘篇
键盘的操作控制: 键盘和鼠标一样是重要输入设备的一部分.开发过程中,会涉及到很多的键盘操作控制. 2.1获取键盘信息 ①获取组合键 Windows中有很多默认的组合键,如Ctrl+v,Ctrl+A.本 ...
- HDOJ 1102 生成树
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- iOS7上在xib中使用UITableViewController设置背景色bug
今天用xcode5.1设置xib中,用静态的方式设置UITableViewController中的tableview,把tableview中的backgroundColor改变后,xib上有效果,但是 ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...