Leetcode First brush _trapping Rain water

Source: Internet
Author: User

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;}    ;


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.