LeetCode42 trapping Rain Water

Source: Internet
Author: User

Topic:

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. (hard)

Analysis:

First consider each location to the left to the right to find the minimum value, and then compare with the value of the location to add water storage results can be done, time complexity O (n^2);

Consider the use of space optimization time, open two arrays, respectively, the current position to the left of the minimum value and the right minimum value.

Iterate through the update Leftheight array one at a time from left to right, one at a time from right to left to update the rightheight array, and the last traversal to calculate result using the above method, but this time it is possible to read the minimum and left values directly from the array.

Code:

1 classSolution {2  Public:3     intTrap (vector<int>&height) {4         intleftmax[height.size ()];5leftmax[0] =0;6          for(inti =1; I < height.size (); ++i) {7Leftmax[i] = max (Height[i-1], Leftmax[i-1]);8         }9         intrightmax[height.size ()];TenRightmax[height.size ()-1] =0; One          for(inti = height.size ()-2; I >=0; --i) { ARightmax[i] = max (height[i +1], Rightmax[i +1]); -         } -         intresult =0; the          for(inti =0; I < height.size (); ++i) { -             if(min (Leftmax[i], rightmax[i])-height[i] >0) { -Result + = (min (leftmax[i], rightmax[i])-height[i]); -             } +         } -         returnresult; +     } A};

The above method optimizes the time complexity to O (n), but takes advantage of additional space and increases the spatial complexity to O (n).

Further optimization, you can consider the idea of the pointers.

The two pointers point to the head and tail, respectively, and maintain two values, representing the maximum leftheight from left to right to P1 and the maximum value rightheight from right to left to P2.

The smaller of the two maximum values moves to the middle, encountering a larger value update of leftheight or Rightheight, and encountering a smaller value to update result.

This allows for time complexity O (n), Space complexity O (1).

Code:

1 classSolution {2  Public:3     intTrap (vector<int>&height) {4         if(Height.size () <2) {5             return 0;6         }7         intleft =0, right = Height.size ()-1;8         intLeftheight = height[0], Rightheight = Height[height.size ()-1];9         intresult =0;Ten          while(Left <Right ) { One             if(Leftheight <=rightheight) { Aleft++; -                 if(Height[left] <leftheight) { -Result + = (Leftheight-Height[left]); the                 } -                 Else { -Leftheight =Height[left]; -                 } +             } -             Else { +right--; A                 if(Height[right] <rightheight) { atResult + = (Rightheight-height[right]); -                 } -                 Else { -Rightheight =Height[right]; -                 } -             } in         } -         returnresult; to     } +};

LeetCode42 trapping Rain Water

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.