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.

 
Example

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

Challenge

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的更多相关文章

  1. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. [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  ...

  3. [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 ...

  4. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  6. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  7. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

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

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

  9. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

随机推荐

  1. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  2. 反弹SHELL

    [姿势] http://www.91ri.org/6620.html http://www.waitalone.cn/linux-shell-rebound-under-way.html [图释] h ...

  3. css 属性

    部分属性在firefox 中添加-moz- safari 以及chrome  加上-webkit- opera 加上-o- ie9里可能需要-ms- jquery  中的css  操作  和一般的cs ...

  4. 如何判断CPU字节序之[Big-endian vs Little-endian]

    [本文链接] http://www.cnblogs.com/hellogiser/p/big-endian-vs-little-endian.html [Big-endian vs Little-en ...

  5. 54. 八皇后问题[eight queens puzzle]

    [本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即 ...

  6. 4.前端笔记之jsdom基础

    一.简介 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.DOM编程: DOM 是关于如何获取.修改.添加或删除 HTML ...

  7. Greedy:Cleaning Shifts(POJ 2376)

      牛的大扫除 题目大意:农夫有N只牛,这些牛要帮助打扫农舍,这些牛只能打扫固定位置(千万要注意这个位置不是连续的),每一段区间必须至少有一只牛打扫,问你至少需要多少只牛?(如果区间不能完全被覆盖,则 ...

  8. [转]处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    今天安装了windows7 开发web项目需要安装IIS,当安装完以后,web程序已经映射到了本地IIS上,运行出现如下错误提示 处理程序“PageHandlerFactory-Integrated” ...

  9. CodeForces - 417A(思维题)

    Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit  ...

  10. NEFU 2016省赛演练一 F题 (高精度加法)

    Function1 Problem:F Time Limit:1000ms Memory Limit:65535K Description You know that huicpc0838 has b ...