[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 ...
随机推荐
- 【福利将至】iPhone用户可用Siri发微信了
北京时间6月14日,苹果WWDC16开发者大会召开.继2015年3月份春季发布会和9月份的秋季新品发布会,苹果和腾讯联手Apple Watch版微信和微信3DTouch功能之后,双方在今年的WWDC上 ...
- Cocos2d-x 3.0修改Android平台帧率fps - 解决游戏运行手机发热发烫问题
使用Cocos2d-x 3.0开发游戏之后,发现游戏在android手机上发热非常严重,在魅族2上,几乎担心手机会爆炸了~~~采取的一个措施就是降低帧率,因为游戏对于帧率要求不是非常高. 做过coco ...
- invert
http://docs.ruby-lang.org/en/2.0.0/Hash.html invert → new_hash Returns a new hash created by using h ...
- Android通过URL加载网络图片
public static Bitmap getBitmap(String path) throws IOException { URL url = new URL(path); HttpURLCon ...
- 静态资源[org.springframework.web.servlet.PageNotFound]
springmvc 无法访问js.css.jpg等资源文件,tomcat启动报警告如下 [org.springframework.web.servlet.PageNotFound] - No mapp ...
- 1-2+3-4+5-6+7......+n的几种实现
本文的内容本身来自一个名校计算机生的一次面试经历,呵呵,没错,你猜对了,肯定 不是我 个人很喜欢这两道题,可能题目原本不止两道,当然,我这里这分析我很喜欢的两道. 1.写一个函数计算当参数为n(n很大 ...
- cURL的几个经典实例
1.cURL请求的基本步骤: (1)初始化 (2)设置选项,包括URL (3)执行并获取HTML文档内容 (4)释放cURL句柄 <?php //1.初始化 $ch = curl_init(); ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- windows下的unix工具集:UnxUtils
参考: http://blog.csdn.net/woohello/article/details/8365639 下载: http://sourceforge.net/projects/unxuti ...