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. 教你50招提升ASP.NET性能(五):确保分页是在数据层完成的

    (11)Make sure paging is conducted at the database layer 招数11: 确保分页是在数据层完成的 When using grid UI contro ...

  2. 修改Oracle Client的字符集

    修改Oracle Client的字符集 找到注册表 HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraDb10g_home1下的 NLS_LANG,修改键值为SIMP ...

  3. Codeforces Round #324 (Div. 2) A. Olesya and Rodion 水题

    A. Olesya and Rodion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/p ...

  4. hdu 5445 Food Problem 多重背包

    Food Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...

  5. Android 设置横屏或竖屏

    方法一:在AndroidManifest.xml中配置 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...

  6. iOS 音频拼接

    工作中或许会遇到这样的需求,将两段不同的音频合成一个音频(暂且称之为音频拼接),实现起来相对来说不是很难,再介绍如何拼接之前,先了解下AVFoundation下的几个基本知识点. 基本知识 AVAss ...

  7. LeetCode144:Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  8. WIndows 7 与 Debian 7 双系统启动引导

    以前写有一篇文章是Windows XP下引导Linux的(http://www.cnblogs.com/ibgo/archive/2012/05/20/2510736.html),采用的是grub4d ...

  9. c/c++指针基础使用

    #include <iostream> #include <string> using namespace std; int main() { "; "; ...

  10. JAVA_HttpClientUtils

    package org.mobiletrain.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStre ...