Very interesting topic, my first thought by the calculation column type maximum area of the problem, I want to find two kinds of columns to meet the specific relationship between the amount of water, the results of various errors, a variety of special circumstances need to be ruled out, I realized that my thinking has a problem.
Stop and think about what the volume of the water is related to? Of course, you can add up all the water, so you have to look at the high and low relations between the two pillars, and consider the height of the bottom. There is also a way to find the area of the whole area, and then to remove the place is not water, the rest is the volume of water. Where is this method? The information required for this method is readily available, and the total area is how to ask for it? A left and right two pointers, and then maintain a current bottom height, if the smallest of the two pointers is higher than the bottom height, then the total area of the entire graph should be added between them beyond the bottom of the section. The one that rises to the bottom is the shorter of the two pillars, the next cycle, moving the shorter column until the two hands meet. What if one or two pointers appear at the bottom of the floor, then it means that he must be submerged in the entire area of the graph, updating the pointer directly for the next round. As for the part to be removed, it is actually a number of pillars, when the value is calculated.
Class Solution {public: int trap (int a[], int n) { if (n < 2) return 0; int l=0, r=n-1, Curlevel=0, all=0, block=0; while (l<=r) { if (min (A[l], a[r]) >curlevel) {all + = (min (a[l], a[r])-curlevel) * (r-l+1); curlevel = min (A[l], a[r]); } if (A[l]<a[r]) block + = a[l++]; else block + = a[r--]; } return all-block;} ;