# Daily algorithm 33: Trapping Rain Water

Source: Internet
Author: User

This is a very interesting problem, to solve the maximum volume problem, it is worth thinking about.

The original question is as follows:

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!

The light color of the figure is the amount of rain, we are not difficult to find that in each column of water content is the highest of the two pillars in the left and right side of the column and the height difference of the current column. So that we can calculate the amount of the breakdown in a successive way. Of course, we can calculate each column at the time to calculate the maximum value of the two sides, but this is a waste of time, we can divide the solution, first solve the highest pillar, so that the column in a certain time as one side of the most value, gradually narrow the range can be. The following is the code of their own, may be messy, but the idea can still feel, in the end will be posted out of the high-quality Daniel wrote the code.

`<span style= "FONT-SIZE:18PX;" >class Solution {public:int max_value (int a,int b) {return a>b?a:b;} int max_element_position (int a[],int begin,int end) {int Max =a[begin];int pos = begin;for (int i = begin+1;i<=end;i++) if (A[i]>max) {max = a[i];p os = i;} return POS;}      int water (int a[],int start,int left_max,int end,int right_max) {int left = 0,right = 0,mid = 0; if (start = = end) {if (Left_max>a[start] && Right_max>a[start]) return min (Left_max,righ        T_max)-A[start];      else return 0;  } int max = Max_element_position (a,start,end);  if (A[max]<left_max && A[max]<right_max)///Here to note that the middle maximum value may also be stored above the water {mid = min (Left_max,right_max)-A[max];      } if (max-1 >= start) left = Water (A,start,left_max,max-1,max_value (Right_max,a[max]));      if (max+1 <= end) right = Water (A,max+1,max_value (Left_max,a[max]), End,right_max);    return left+right+mid; } int Trap (int a[], inT n) {if (n<=2) return 0;    Return water (a,0,0,n-1,0); }};</span>`
The following method is really very understanding a lot, good good.

`int trap (int a[], int n) {        //Note:the Solution object is instantiated only once.        if (A==null | | n<1) return 0;    int maxheight = 0;vector<int> leftmostheight (n); for (int i =0; i<n;i++) {Leftmostheight[i]=maxheight;maxheight = maxheight > A[i]? Maxheight:a[i];} MaxHeight = 0;vector<int> rightmostheight (n); for (int i =n-1;i>=0;i--) {rightmostheight[i] = MaxHeight; MaxHeight = maxheight > A[i]? Maxheight:a[i];}  int water = 0;for (int i =0; i < n; i++) {int high = min (Leftmostheight[i],rightmostheight[i])-a[i];if (high>0) water + = High;} return water;    }`

Related Keywords:
Related Article

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.