"Leetcode" trapping Rain water

Source: Internet
Author: User

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 .

I do the topic, generally in accordance with the AC rate sorting selected to do, this problem is not careful, the difficulty incredibly or hard, well, it is difficult to me. The AC rate is actually quite high, I also submitted 3 times after the AC. First of all, let me tell you the wrong way: for the first time, I was on each point, looking for the longest ascending order on both sides, and then taking the two sequences of the left and right sides as a pit to fill the rainwater. But for [5,1,2,1,2,1,2,5] Such a sequence, that is, in the interval inside the lift, actually does not affect its water , so wa! The second personal thought also can be completed, but need to consider more, more annoying, not the most appropriate method, the method is to scan from left to right, sweep to the first higher than the current, as an interval, and then from the end of the interval to continue to sweep, and so on, this method can solve the problem above, but The last interval needs to be considered separately .

In fact, this can be solved: first find the highest problem maxhigh, and then separate from both sides to this node traversal, set a secondary high Temphigh,

If the current node is higher than the secondary high, then update the secondary highs, continue;

Otherwise, the current node is subtracted from the secondary highs, which is the volume of water we can hold.

It is so simple and rough, the only flaw is that it takes two scans, obsessive-compulsive or the pursuit of a perfect idea, you can look at a method on the Internet, directly from both sides to start scanning, once done. Personal Sense code doesn't look intuitive enough.

Put my own code below:

Class solution:    # @param A, a list of integers    # @return An integer    def trap (self, A):        res = 0        If Len ( A) < 3:            return 0        maxv = 0        maxid = 0 for        i in range (len (A)):            if a[i] > MAXV:                maxv = A[i]
   maxid = i        temphigh = 0 for        j in Range (MAXID):            if A[J] > Temphigh:                temphigh = a[j]            else:
   
    res + = Temphigh-a[j]        temphigh = 0 for        j in Range (Len (A) -1,maxid,-1):            if A[J] > Temphigh:                temphig h = a[j]            Else:                res + = Temphigh-a[j]        return res
   

  

"Leetcode" 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.