[42] leetcode rainwater (stack pointer bis)

Topic links: https://leetcode-cn.com/problems/trapping-rain-water/

Title Description

Given n each represents a non-negative integer of 1 column width height map calculated Click column arrangement, then able to take much rain rain.

Here Insert Picture Description

The above is an array [0,1,0,2,1,0,1,3,2,1,2,1] FIG highly represented, in this case, can take six units rainwater (blue portion represents the rain). Thank Marcos contribution to this figure.

Example:

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

Thinking

1 violence

For each element in the array, the highest position achieved after finding rain water, the smaller value is equal to the maximum height on both sides of the current value minus the height of

Complexity Analysis

  • Time complexity: O (n ^ 2)
  • Space complexity: O (1)
class Solution {
public:
    int trap(vector<int>& height) {
        int ret = 0;
        int len = height.size();
        for (int i = 1; i < len -1; ++i) {
            int maxLeft = height[i], maxRight = height[i];
            for (int j = i-1; j >=0 ; --j)
                maxLeft = max(maxLeft,height[j]);       // 找到左边最大值
            for (int j = i+1; j < len;++j)
                maxRight = max(maxRight,height[j]);     // 找到右边最大值
            ret += min(maxLeft,maxRight) - height[i];   // 两边最大高度的较小值减去当前高度的值
        }
        return ret;
    }
};

Here Insert Picture Description

2 Dynamic Programming

In the violent method, we find the maximum every time just to the left and right scan again. But we can store this value in advance. Therefore, it can be solved by dynamic programming.

Each element is stored in advance the maximum left and right maximum.

Complexity Analysis

  • Time complexity: O (n)
  • Space complexity: O (n)
class Solution {
public:
    int trap(vector<int>& height) {
        if(height.size() < 3)
            return 0;
        int ret = 0;
        int len = height.size();
        vector<int> maxLeft(len,0), maxRgiht(len,0);
        maxLeft[0] = height[0], maxRgiht[len-1] = height[len-1];
        for (int i = 1; i < len; ++i)
            maxLeft[i] = max(maxLeft[i-1], height[i]);
        for (int i = len-2; i>=0 ; --i)
            maxRgiht[i] = max(maxRgiht[i+1], height[i]);
        for (int i = 0; i < len; ++i)
            ret += min(maxLeft[i], maxRgiht[i]) - height[i];
        return ret;
    }
};

Here Insert Picture Description

3 decreasing stack

We can not be like storage method as maximum height, but with a stack to keep track of the longest bar block may store water. Calculated using the stack can be completed in one pass through.

We maintain a stack while traversing the array. If the current bin of the stack is less than or equal to the stripe block, we block index bar stack, meaning that the current block is the front bar defining a bin of the stack. We find that if a bar is longer than the top of the stack block, we can determine the bin of the stack is defined before a bin of the current block and the bar stack, so we can answer pop the top element and to accumulate ret.

Four pairs pointer

Method 2 and compared, we do not calculate the left and right separately, we think of ways at once traversal. From the schematic dynamic programming method we note that, as long as the right_max[i]>left_max[i](element 0 to the element 6), the height of the water will be left_maxdetermined, similarly left_max[i]>right_max[i](element 11 to element 8).

Therefore, we can think that if a higher bin of one end (e.g. the right), the water is highly dependent on the current height direction (left to right). We found that when the height of the block bar on the other side (right side) is not the highest, then we start traversing in the opposite direction (from right to left). We must maintain while traversing left_maxand right_max, but we can now use two hands alternately, to achieve once traversed to complete.

Note : while in the judging condition if (height[left] < height[right])controls if left there is a large value, the current maximum amount of rainwater depends on the right!

Complexity analysis:

  • Time complexity: O (n). Single pass through a time O (n).
  • Complexity Space: extra space O (1) is.
class Solution {
public:
    int trap(vector<int>& height) {
        if(height.size() < 3)
            return 0;
        int ret = 0;
        int maxLeft = 0, maxRight = 0;
        int left = 0, right = height.size() -1;
        while(left < right){
            if (height[left] < height[right]){     
                if (height[left] > maxLeft)
                    maxLeft = height[left];
                else
                    ret += maxLeft - height[left];
                ++ left;
            }else{
                if (height[right] > maxRight)
                    maxRight = height[right];
                else
                    ret += maxRight - height[right];
                -- right;
            }
        }

        return ret;
    }
};

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/zjwreal/article/details/94841882
Recommended