题目:

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.

题解:

  读题可知,区间[a,b]内容积由边缘最小值决定,Two pointer思想,遍历整个数组。每一次都是寻找两端最小值,然后开始遍历,遇到更大值后替换端值(不能盛水了)。

Solution 1 () (from here 九章算法)

class Solution {
public:
int trapRainWater(vector<int> &heights) {
int left = , right = heights.size() - ;
int res = ;
if (left >= right) {
return res;
}
int lheight = heights[left];
int rheight = heights[right];
while (left < right) {
if (lheight < rheight) {
++left;
if (lheight > heights[left]) {
res += lheight - heights[left];
} else {
lheight = heights[left];
}
} else {
--right;
if (rheight > heights[right]) {
res += rheight - heights[right];
} else {
rheight = heights[right];
}
}
}
return res;
}
};

  其他解法见此处

【Lintcode】363.Trapping Rain Water的更多相关文章

  1. 【Lintcode】364.Trapping Rain Water II

    题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...

  2. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  3. 【LeetCode】042 Trapping Rain Water

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

  4. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  5. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  6. 【LeetCode题意分析&解答】42. Trapping Rain Water

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

  7. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

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

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

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

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

随机推荐

  1. A/B(逆元)

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. E - What Is Your Grade?

    E - What Is Your Grade? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. jvm本身的多线程机制

    1 多线程环境下的构造函数调用 构造函数本身并没有隐式的同步,因为各个线程构建的是自己的对象,它们之间是不存在竞争关系的. 2 class loader在load class时被了sychronize ...

  4. 广告 竞价排名 import Levenshtein as Le seqratio_res = Le.seqratio(chk_name_lsit, cmp_)

    pip install python-Levenshtein from openpyxl import Workbook import xlrd import time import Levensht ...

  5. 【python】-- MySQL简介、安装、操作

    MySQL简介.安装.操作 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储 ...

  6. cocos2dx的ui封装

    cocos2dx里加载cocosudio导出的ui配置文件,在这之上封装了一下,封装核心类包括 UIManager,UILayer,UIOwner UIManager是所有ui总的管理类,代码如下: ...

  7. 教你管理SQL数据库系列(1-4)

    原文 教你管理 SQL Server 数据库(1)数据库的结构  http://bbs.51cto.com/thread-1084951-1.html教你管理 SQL Server 数据库(2)系统数 ...

  8. 【Flask】filter 常用查询条件

    1. equal    2. not equal  3. like, ilike  4.in  5. not in 6. is null  7. is not null  8. and  9. or ...

  9. python内置方法补充bin

    bin(x) 英文说明:Convert an integer number to a binary string. The result is a valid Python expression. I ...

  10. Docker 架构篇

    Docker 的核心组件包括: Docker 客户端 - Client Docker 服务器 - Docker daemon Docker 镜像 - Image Registry Docker 容器 ...