Leetcode42 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 rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

题目意思:就是给很多高度,然后看最多能形成多少雨水。

思路:
方法一首先是暴力,就是找出自己左边的高度最高值,然后找出右边高度的最高值,因为能盛多少水取决于矮的那边,所以在i处能盛多少水等于(max_left - max_right) - height[i]。其实也就是在竖直方向依次找到能取多少水 所以时间复杂度为O(n^2)

代码:

class Solution {
public:
    int trap(vector<int>& height) 
    {
        int size = height.size();
        int ans = 0;
        int max_left,max_right;
        for(int i=1;i<size-1;i++)
        {
            max_left = 0,max_right = 0;
            for(int j=i;j>=0;j--)
                max_left = max(max_left,height[j]);
            for(int j=i;j<size;j++)
                max_right = max(max_right,height[j]);
            ans += (min(max_left,max_right) - height[i]);
        }
        return ans;
    }
};

方法二:用栈的方法 我觉的是一种水平方向来存水的方法。栈里面永远是一个单调不增的序列,如果当前将要入栈的大于栈顶的高度,例如栈里代表的高度为2 1 然后进入3,那么这时候2 1 3 这个高度就可以形成一个凹槽,就可以储水。当然如果栈里只有一个元素,那么就形成不了凹槽,则直接弹出。

代码:

class Solution {
public:
    int trap(vector<int>& height) 
    {
        stack<int>st;
        int ans = 0;
        int size = height.size();
        int cur = 0;
        while(cur<size)
        {
            while(!st.empty()&&height[cur]>height[st.top()])
            {    
                int top = st.top();
                st.pop();
                if(st.empty())  break;
                int distance = cur - st.top() -1 ;
                int h = min(height[cur],height[st.top()]) - height[top];
                ans  += h * distance;
            }
            st.push(cur++);
        }
        return ans;
    }
};

那么时间复杂度是O(n)

方法三:动态规划
其实我们的方法一普通的暴力,其实没有必要每次都比一次,例如得到左边的最高高度,那么如果在i-1处max_left = 3,那么在i处的max_left只需要和height[i]进行比较即可。同理,右边也是如此。那么
代码:

class Solution {
public:
    int trap(vector<int>& height) 
    {
        if(height.empty())  return 0;
        int size = height.size();
        int ans = 0;
        vector<int> max_left(size),max_right(size);
        max_left[0]  = height[0];
        for(int i=1;i<size;i++)  max_left[i] = max(max_left[i-1],height[i]);
        max_right[size-1]  =  height[size-1];
        for(int i=size-2;i>=0;i--)  max_right[i]  = max(max_right[i+1],height[i]);
        for(int i=1;i<size-1;i++) ans += (min(max_left[i],max_right[i])-height[i]);
        return ans;
    }
};

时间复杂度为O(n)

方法四:Two pointers

其实我们在用动态规划解决这个问题的时候,没有必要从左到右和从右到左扫描扫面,其实如果左边的高度比右边的高度小,然后扫描的这个位置的当前高度又比它左边的最大高度矮,那么就形成了凹槽,加起来就行,注意怎么保证的right_max>left_max>height[i],也就是形成凹槽的原因
因为我们是如果height[left] < height[right]时left才++的,所以一定能保证右侧有个点的高度能大left_max

代码:

class Solution {
public:
    int trap(vector<int>& height) 
    {
        if(height.empty()) return 0;
        int size = height.size();
        int left_max,right_max;
        left_max = right_max = 0;
        int left = 0,right = size-1;
        int ans = 0;
        while(left<right)
        {
            if(height[left]<height[right])
            {
                if(height[left]>left_max) left_max = height[left];
                else ans += left_max-height[left];
                left++;
            }
            else 
            {
                if(height[right]>right_max)  right_max = height[right];
                else ans += right_max-height[right];
                right--;
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值