Https://leetcode.com/problems/trapping-rain-water/#/solutions
Here's my idea:instead of calculating area by Height*width, we can think it in a cumulative. In the other words, the sum water amount of each bin (width=1).
Search from left to right and maintain a max height of left and right separately, which was like a one-side wall of partial Container. Fix the higher one and flow water from the lower part. For example, if current height of lower, we fill water in the left bin. Until left meets right, we filled the whole container.
Left and right pointers, maximum maintenance, positive sequence, reverse order
Class Solution {public: int trap (int a[], int n) { int left=0; int right=n-1; int res=0; int maxleft=0, maxright=0; while (left<=right) { if (A[left]<=a[right]) { if (a[left]>=maxleft) maxleft=a[left]; else Res+=maxleft-a[left]; left++; } else{ if (a[right]>=maxright) maxright= a[right]; else Res+=maxright-a[right]; right--; } } return res; }};
Trapping Rain Water