leetcode problem 42 -- 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.

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)

 1 class Solution {
 2 public:
 3     int trap(vector<int>& height) {
 4         vector<int> leftToRight;
 5         vector<int> rightToLeft;
 6         aux_function(height.begin(), height.end(), leftToRight);
 7
 8         aux_function(height.rbegin(), height.rend(), rightToLeft);
 9
10         int ans = 0;
11         auto it1 = leftToRight.begin();
12         auto it2 = rightToLeft.rbegin();
13
14         while (it1 != leftToRight.end()){
15             ans += min(*it1++, *it2++);
16         }
17         return ans;
18     }
19
20 private:
21 template<class Iter>
22     void aux_function(Iter begin, Iter end, vector<int> &ret) {
23         int left = 0;
24         for (auto it = begin; it != end; it++) {
25             left = left > *it ? left : *it;
26             ret.push_back(left - *it);
27         }
28     }
29 };

  思路二:

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

代码:(runtime 10ms)

  

class Solution {
public:
    int trap(vector<int> A) {
        int n = A.size();
        int summap = 0;
        int sumtot = 0;

        for(int i = 0; i < n; i++) summap += A[i];

        int left = 0, right = n - 1;
        int leftbar = 0, rightbar = 0;
        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;
    }
};

时间: 2024-10-10 02:50:41

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

[Leetcode][Python]42: Trapping Rain Water

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 42: Trapping Rain Waterhttps://oj.leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing an elevation map where the width of each bar is 1,compute how much water

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

一天一道LeetCode系列 (一)题目 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 m

No.42 Trapping Rain Water

1 class Solution 2 { 3 public: 4 int trap(vector<int> &height) 5 {/* 6 雨水覆盖问题: 7 每一个bar能承受的水量为:它左侧所有中最高的和右侧所有中最高的中取最小值作为一个瓶颈[否则也留不住], 8 若该值大于当前bar的高度,其差值即为所求 9 累加所有bar能承受的水量即为所求 10 法三:[左右两个指针] 11 找到最高的位置,将数组分为两部分: 12 对左侧数据,因为右边已经有了最高值,所以,类似法一,找到

42. Trapping Rain Water(js)

42. 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. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].

刷题42. Trapping Rain Water

一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的解法 这个题目是找"坑",然后计算里面可以存的"雨".总共提交了5次,前面几次都是边界错误. 代码如下: #include<iostream> #include<vector> using namespace std; class Solutio

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

11 Container With Most Water 42.Trapping Rain Water

11 和 42 本质上都是木桶原理: 11 如何才能存最多的水? 假设 a[left] < a[right] ,    total = a[left] *(right-left) ,  那么 right -1, right-2 位置 都比 total 小, 此时就没必要move right 了, 因为所有的right -x 都比 right 小. 此时只需要move left 指针, 继续找最大value. code 很简单: public int maxArea(int[] height) {

19.2.4 [LeetCode 42] 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. 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

42. 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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented