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!

思路:

  这一道题类似于leetcode 11 题Container With Most Water.  要求最多能装多少水.有两种思路.

  思路一:

    先从左往右遍历.每遍历一个数字,就贪心得到这个数字所在的格子能最多装水. 由于是从左到右遍历,我们只考虑的左边的高度,没有考虑右边的高度(实际应该选择两者小的那个). 因此需要再从右往左遍历,同样贪心这个数字所在格子

  最多能装多少水,这次以右边高度为准. 最终综合从左到右和从右到左的结果,取两者小的.

代码:(runtime 19ms)

 class Solution {
public:
int trap(vector<int>& height) {
vector<int> leftToRight;
vector<int> rightToLeft;
aux_function(height.begin(), height.end(), leftToRight); aux_function(height.rbegin(), height.rend(), rightToLeft); int ans = ;
auto it1 = leftToRight.begin();
auto it2 = rightToLeft.rbegin(); while (it1 != leftToRight.end()){
ans += min(*it1++, *it2++);
}
return ans;
} private:
template<class Iter>
void aux_function(Iter begin, Iter end, vector<int> &ret) {
int left = ;
for (auto it = begin; it != end; it++) {
left = left > *it ? left : *it;
ret.push_back(left - *it);
}
}
};

  思路二:

    如上图中所示, 先求黑色格子与蓝色格子的总面积,然后再减去黑色各自面积.

代码:(runtime 10ms)

  

class Solution {
public:
int trap(vector<int> A) {
int n = A.size();
int summap = ;
int sumtot = ; for(int i = ; i < n; i++) summap += A[i]; int left = , right = n - ;
int leftbar = , rightbar = ;
while(left <= right) {
leftbar = max(A[left], leftbar);
rightbar = max(A[right], rightbar); if(leftbar <= rightbar) {
sumtot += leftbar;
left++;
//right--;
} else {
sumtot += rightbar;
right--;
//left++;
}
} return sumtot - summap;
}
};

leetcode problem 42 -- Trapping Rain Water的更多相关文章

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

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

  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】#42. Trapping Rain Water

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

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

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

  5. LeetCode OJ 42. Trapping Rain Water

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

  6. LeetCode - 42. Trapping Rain Water

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

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

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

  8. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

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

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

随机推荐

  1. MSSQL 如何实现 MySQL 的 limit 查询方式 (转)

    不知为何,MSSQL 中没有 limit 这个极为重要的查询方式,熟悉 MySQL 的朋友都知道,MySQL 的 limit 对于实现分页和一些限制结果集的应用中非常方便.没有不要紧,我们可以用其他方 ...

  2. maven配置编译路径

    在build标签下添加 <build> <sourceDirectory>src/main/java</sourceDirectory> <resources ...

  3. [Bootstrap] 8. 'Collapse', data-target, data-toggle & data-parent

    Using Bootstrap JavaScript Plugins If we want to add behavior to our website, which of the following ...

  4. ios开发——实用技术篇Swift&Swift调用C、C++、Object

    Swift调用C.C++.Object 1.Swift调用C语言a,首先在项目中添加 CFile 文件命名为CHello,同时产生桥梁文件. b,创建之后的项目结构 b,在CHello.h文件中编写接 ...

  5. eclipse @override错误

    @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现都可以加上@Over ...

  6. c#_delegate_异步调用_BeginInvoke

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Mechanism of Loading Resources

    Mechanism of Loading Resources 1. Distributed strategy 1.1. Developer guilde 1.2. Notes 2. Centraliz ...

  8. 判断ie,并确定其版本号

    var UA = navigator.userAgent,isIE = UA.indexOf('MSIE') > -1,v = isIE ? /\d+/.exec(UA.split(';')[1 ...

  9. Service的启动与停止、绑定与解绑

    ---恢复内容开始--- Service的意义就在于当软件停止之后还可以在背景中进行运行,换句话也就是说,比如一个音乐播放器,当我们退出音乐播放器的时候,还是希望它在背景中运行,也就是一直播放着音乐, ...

  10. C语言结构体的引入

    #include <stdio.h> struct student{ int ID; ]; int age; }; int main(){ //赋值: , }; ,.name=}; , , ...