天天看点

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.
leetcode-42. Trapping Rain Water 接雨水

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 Marcosfor contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
           
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
leetcode-42. Trapping Rain Water 接雨水

上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。

示例:

输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6
           

思路:这道题目 类似于直方图中的 最大矩形和盛水最多的容器。第一种方法就是,i和j分别从前往后和从后往前遍历直到i>=j,看他们对应的 值哪个小,若height[i]<=height[j],那就从左往右如果遇到的 高度小于当前的 高度,加上height[i]与当前高度的差;若height[i]>height[j],那就从右往左如果遇到高度小于当前高度,则res加上height[j]与挡圈高度的差。最终res就是雨水容量。比如题目中从前往后height[2]<height[1],那res+=1,(注释height[1]-height[2]),接着从后往前height[9]<height[10],res+=1,(注释height[10]-height[9]),接着从前往后height[4]<height[3],res+=1,(注释,height[3]-height[4]),接着继续Height[5]<height[3],res+=2,(注释,height[3]-height[5]),接着继续往后height[6]<height[3],res+=1,(注释,height[3]-height[6])。下面是第一种代码实现

第二种方法是使用DP。我们维护一个一维的dp数组,这个DP算法需要遍历两遍数组,第一遍遍历dp[i]中存入i位置左边的最大值,然后开始第二遍遍历数组,第二次遍历时找右边最大值,然后和左边最大值比较取其中的较小值,然后跟当前值A[i]相比,如果大于当前值,则将差值存入结果

class Solution {
public:
    int trap(vector<int>& height) {
        int res = 0, mx = 0, n = height.size();
        vector<int> dp(n, 0);
        for (int i = 0; i < n; ++i) {
            dp[i] = mx;
            mx = max(mx, height[i]);
        }
        mx = 0;
        for (int i = n - 1; i >= 0; --i) {
            dp[i] = min(dp[i], mx);
            mx = max(mx, height[i]);
            if (dp[i] > height[i]) res += dp[i] - height[i];
        }
        return res;
    }
};
           

第三种是用栈。

二三种方法参考:http://www.cnblogs.com/grandyang/p/4402392.html

class Solution {
public:
    int trap(vector<int>& height) {
        int res=0,i=0,j=height.size()-1;
        while(i<j)
        {
            int small=min(height[i],height[j]);
            if(small==height[i])
            {
                ++i;
                while(i<j && height[i]<small)
                    res += small - height[i++];
            }else
            {
                --j;
                while(i<j && height[j]<small)
                    res +=small - height[j--];
            }
        }return res;
    }
};