Leetcode 42. Trapping Rain Water

来源:互联网 发布:网络控制机柜价格 编辑:程序博客网 时间:2024/05/02 19:00
题目

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!

思路

本人的解法比较粗暴,先找出所有可以装水的边界的位置,然后每次找两个边界,比较左右边界大小,较小的就是容器高度,找到容器高度再求储水量就很简单了。

如何找到所谓的边界的位置:

temp选定为第1个数,从头开始扫描给定的数组,当第i个数比temp大时,将i保存到front_scan数组中,并将temp变为第i个数。

此时front_scan保存的是最前面几个边界的位置。

类似地,temp为最后一个数,从后开始扫描给定数组,当第i个数比temp大时,将i保存到back_scan数组中,并将temp变为第i个数。稍有不同的是,当i等于front_scan的最后一个值时,就可以结束循环。

此时back_scan保存的是最后面几个边界的位置。

然后将两次扫描的结果合并,就能得到所有边界的位置。

代码:

class Solution {public:    int trap(vector<int>& height) {        int highest, water = 0;        vector<int> front_scan, back_scan;        int temp, key = 0;        if(height.size() > 0)        {            temp = height[0];            front_scan.push_back(0);        }        //找前面的边界位置         for(int i = 1; i < height.size(); i++)        {            if(height[i] >= temp)            {                temp = height[i];                front_scan.push_back(i);                key = i;            }        }        int size = height.size()-1;        if(size >= 0)            temp = height[size];        //找后面的边界位置         for(int i = size; i >= 0; i--)        {            if(i == key)                break;            if(height[i] >= temp)            {                temp = height[i];                back_scan.push_back(i);            }        }        size = back_scan.size()-1;        //合并         for(int i = size; i >= 0; i--)        {            front_scan.push_back(back_scan[i]);        }        size = front_scan.size()-1;        //求储水量         for(int i = 0; i < size; i++)        {     //求出容器高度             highest = height[front_scan[i]] > height[front_scan[i+1]] ?                              height[front_scan[i+1]]:height[front_scan[i]];            for(int j = front_scan[i]+1; j < front_scan[i+1]; j++)            {                water += highest - height[j];            }        }        return water;    }};

时间复杂度:

代码中出现一个两重循环,但实际操作最多为n-2,n是数组height的长度。所以整个时间复杂度是O(n)。

0 0
原创粉丝点击