[Leetcode]101. Trapping rain water collect rainwater

Source: Internet
Author: User

Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much WA ter It is the 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 the Rain Water (blue section) is being trapped. Thanks Marcos for contributing this image!

Subscribe to see which companies asked this question

Solution 1: Iterate through the array and find the local minimum value, either if the current value is greater than or equal to the previous value, or if the current value is greater than the last value. After finding the local minimum value, then left to the left to find the maximum value, and then to the right to the maximum value, to find the right maximum value when it is larger than the left to stop looking. Then figure out the amount of water that can be loaded from the left maximum to the right maximum, and then continue to find the local minimum from the position of the right maximum, and so on until the complete array is traversed.
classSolution { Public:    intTrap (vector<int>&height) {        intSumtrap =0, n = height.size (), left =1, right =0;  for(inti =1; I < n-1; ++i) {if(Height[i] >= height[i-1] || Height[i] > Height[i +1])Continue;  for(left = i-1; Left >0; --Left ) {                if(Height[left] >= height[left-1]) Break; } Right= i +1;  for(intj = i +1; J < N; ++j) {if(Height[j] >=Height[right]) { Right=J; if(Height[right] >= Height[left]) Break; }            }            inth =min (Height[left], height[right]);  for(intK = left +1; K < right; ++k) {if(H > Height[k]) sumtrap + = h-Height[k]; } I=Right ; }        returnSumtrap; }};

Solution 2: For each value, and the other two values of the container is collected by the largest amount of rainwater is definitely in the left and right side of the maximum as the container's two walls, in the case of the problem is the difference between the smaller of the two maximum value and the current value (when the minimum value is greater than the current value, otherwise not collect rainwater) Initializes a one-dimensional array of vector<int> DP (n,0) using dynamic planning. Then traverse the array two times, the first traversal from the left scan to find the current position to the left of the maximum value, and stored in the DP, the second traversal from the right side of the scan to find the current position to the right of the maximum value, and then the DP in the left side of the maximum value, save the lower of the two values, and compare this If it is greater than the current value, the total amount of rainwater collected should be added to the difference between this small value and the current value.

classSolution { Public:    intTrap (vector<int>&height) {        intn = height.size (), Left_max =0, Right_max =0, Sum_trap =0; Vector<int> DP (N,0);  for(inti =0; I < n; ++i) {Dp[i]=Left_max; Left_max=Max (Left_max, Height[i]); }         for(inti = n-1; I >=0; --i) {Dp[i]=min (dp[i], Right_max); Right_max=Max (Right_max, Height[i]); if(Dp[i] > Height[i]) sum_trap + = Dp[i]-Height[i]; }        returnSum_trap; }};

Solution 3: According to the above analysis of rainwater collection, any 3 values of rainwater collection is the difference between the smaller and the median value of the two sides (if the former is greater than the latter). Therefore, you can set two pointers to traverse from both ends to the middle, which is the value of the two walls of the container. Low walls (smaller values) determine the amount of rainwater collected, so the median value is set to a subsequent value from a smaller value, and the collected rainwater is calculated until a value greater than this small value is encountered and the smaller value is updated to the current value. This will only take a single scan to solve the problem.

classSolution { Public:    intTrap (vector<int>&height) {        intn = height.size (), left =0, right = N-1, Sum_trap =0;  while(Left <Right ) {            intMin_value =min (Height[left], height[right]); if(Height[left] = =min_value) {                ++Left ;  while(Height[left] < Min_value) Sum_trap + = min_value-height[left++]; }            Else {                --Right ;  while(Height[right] < Min_value) Sum_trap + = min_value-height[right--]; }        }        returnSum_trap; }};

[Leetcode]101. Trapping rain water collect rainwater

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.