LeetCode: Trapping Rain Water [041]

【题目】

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!

【题意】

给定一个数组,数组中每一个索引位表示一个bar, 对应索引位的值表示bar的高度,bar的宽度为1。计算这个bar组能盛多少水。

【思路】

如图所示,次高和最高之间可以构成盛水区域。因此我们的目的就是找到所有这样的凹槽区域;

以A[0]为凹槽的左边界,我们需要向后扫描找到第一个不低于A[0]的bar, 假设即为A[k], 那么A[0]和A[k]之间就构成了一个凹槽;

然后以A[k]为新凹槽的左边界,向后扫描找到第一个不低于A[k]的bar, 假设即为A[m], 那么A[k]和A[m]之间就构成了一个凹槽;

依次向后类推。

【代码】

class Solution {
public:
    int trap(int A[], int n) {
        if(n<3)return 0;
        int result=0;
        int left=0;
        int right=0;
        int maxHeight=0;    //用来保存左边界左边的最大高度
        int maxIndex=-1;     //用来保存左边界左边最大高度对应的索引位置
        while(left<n-1){
            right=left+1;
            maxHeight=0;
            while(right<n && A[right]<A[left]){
                if(A[right]>=maxHeight){
                    maxHeight=A[right];
                    maxIndex=right;
                }
                right++;
            }
            if(right>=n){
                //如果没有找到不低于左边界的右边界,则取次高bar计算
                right=maxIndex;
            }
            //计算当前凹槽的储水量
            int height=min(A[left], A[right]);  //获得储水高度
            for(int i=left+1; i<right; i++){
                result+=height-A[i];
            }
            left=right;
        }
        return result;
    }
};

LeetCode: Trapping Rain Water [041]

时间: 2024-12-28 20:28:51

LeetCode: Trapping Rain Water [041]的相关文章

[leetcode]Trapping Rain Water @ Python

原题地址:https://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 it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,

[LeetCode] [Trapping Rain Water 2012-03-10]

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 a

LeetCode: Trapping Rain Water 解题报告

https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven 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,

[LeetCode] Trapping Rain Water II 收集雨水之二

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th

LeetCode——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 a

Leetcode: Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th

[LeetCode] 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 a

[LeetCode] Trapping Rain Water

Note: The following idea is in fact from the last answer in this link, which leads to a clean code. I just reorganize it and add some explanations. I hope it is Ok. The following are four solutions in C/C++/Java/Python respectively. The basic idea is

leetcode Trapping Rain Water pthon

class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 for i in range(len(nums)): if nums[i] > leftmax: leftmax=nums[i] leftmosthigh[i] = leftmax print leftmosthigh sums=0 rightmax=0 for i in reversed(rang