LeetCode a daily question 42. rainwater more detailed description C ++ solution to a problem

LeetCode a daily question 42. rainwater

Hello everybody, my name is official Qi Jie (qí guān jié), drip CSDN recorded in the course of learning, time flies, the future can be, come together it ~

Difficulty 2020.04.04 Daily question

topic

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).
Example:
  Input: [0,1,0,2,1,0,1,3,2,1,2,1]
  Output: 6

Solution one: violence Solution

  First, we analyze the topic, give me a title vector<int> height, a record height of each cylinder, let us ask how much of this section of the cylindrical water can be stored, the width of each cylinder is 1. See this topic first idea is violence Dafa (subject write less, the level is not enough ..), violence, we only need to traverse each column, and then calculated the highest post he is currently in front of the cylinder height and maximum height of the rear cylinder, which we take the highest value of the smaller one (bucket can hold much water depends on at least one side), then we use one of these small height subtracting the current height of the cylinder, that is, current cylinder can store water. We traverse all cylinders, the cylinder cumulative values of these storage that is our answer.
  Such as the example of the figure, when I = 4, it is left up height[3] = 2, right up to height[7] = 3the minimum value of two 2, subtracting the current height of the cylinder height[4] = 11, so the current can be cylindrical reservoir 1.
Detailed code:

class Solution {
public:
    int trap(vector<int>& height) {
        int ans = 0;
        int length = height.size();
        int max_left,max_right;
        for(int i = 0; i< length; i++){
            max_left = max_right = 0;
            //找到左面的最高值
            for(int j = i; j >= 0; j--){
                if(height[j] > max_left)
                    max_left = height[j];
            }
            //找到右面的最高值
            for(int j = i; j < length; j++){
                if(height[j] > max_right)
                    max_right = height[j];
            }
            // 当前柱面能够接的雨水为:左右两名最高值的低值,然后减去当前柱面的高度
            ans += min(max_right,max_left) - height[i];
        }
        return ans;
    }
};

Efficiency at this time is O (n ^ 2), running time:
Here Insert Picture Description

Solution two: dynamic programming

  In the solution of a violent solution, we found that every time I traversed to the current cylinder, one needs to calculate the left and right of the highest cylinder, where we optimize it, to use two arrays were recorded from the beginning on both sides, for of different current maximum i. Each traverse can save time for us, then we can be our time complexity (n ^ 2) optimization from O to O (n). We do not underestimate this small change, after this change, improve time efficiency direct an order of magnitude, paste the following code efficiency and execution, so that we feel for.

class Solution {
public:
    int trap(vector<int>& height) {
        int ans = 0;
        int length = height.size();
        //空vector时直接返回0
        if(length == 0)
            return 0;
        int max_left[length],max_right[length];
        //i = 0时,左面(含当前)最大值为height[0]
        max_left[0] = height[0];
        for(int i = 1; i < length; i++){
            //max_left[i] 赋值为当前柱面的高度和max_left[i-1]最大值
            max_left[i] = max(height[i],max_left[i-1]);
        }
        //i = length -1 时,右面(含当前)最大值为height[length-1]
        max_right[length-1] = height[length-1];
        for(int i = length-2; i >= 0; i--){
            //max_right[i]赋值为当前柱面的值和max_right[i+1]的最大值
            max_right[i] = max(height[i],max_right[i+1]);
        }
        for(int i = 0; i< length; i++){
            // 当前柱面能够接的雨水为:左右两面最高值的低值,然后减去当前柱面的高度,赋值为temp
            int temp = min(max_right[i],max_left[i]) - height[i];
            // 如果temp>0,则ans进行累加,否则加0
            ans += temp > 0 ? temp : 0;
        }
        return ans;
    }
};

Efficiency would be:
Here Insert Picture Description
Use only a 4ms, compared to the violent solution of 413ms full 100-fold increase efficiency, the application of dynamic programming is very broad, can greatly improve the efficiency of the algorithm.
  This is a problem today, daily content, and we Come forward with temper ~

Published 169 original articles · won praise 857 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_43422111/article/details/105315208