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 .

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!

And the previous question container with the most water is a very similar topic. But the problem is not to find two boundaries to make it the largest area, but requires all the area of rain, so it is very different.

There are a number of solutions, one based on the stack, and another two scans, to get the highest columns on the left and right sides of each pillar, and the rainwater that each pillar can hold is min (max_left,max_right). This is the only way to count one pillar at a time. Stack's approach is to calculate the amount of water stored horizontally (or at multiple level) each time.

The code for two scans is well understood, and my implementation is as follows:

classsolution (object):defTrap (self, height):""": Type Height:list[int]: Rtype:int"""                if  notHeightorLen (height) < 3:            return0 N=len (height) left=[height[0]] right= [Height[-1] # to complete a sweep of the left and right highest values at once forIinchXrange (1, N):ifHeight[i-1] > Left[-1]: left.append (height[i-1])            Else: Left.append (left[-1])                            ifHeight[n-i] > Right[-1]: right.append (height[n-i])Else: Right.append (right[-1]) Water=0
# Calculate Area forIinchXrange (1,n-1): Res= Min (Left[i],right[~i])-Height[i]ifRes >0:water+=ResreturnWater

The time complexity of the above approach is O (n) and the spatial complexity is O (n), which can be reduced to O (1) using double pointers, as described in Leetcode discuss, which I haven't read yet.

Stack-based approach is to use the stack to calculate a descending sequence, for the top of the stack, the second-to-last element in the stack is higher than it, once the current traversal of the element is higher than the top of the stack, you can calculate the stack top this bar for bottom can store the horizontal amount of water. All elements will be executed one time into the stack. The code is as follows:

classsolution (object):defTrap (self, height):""": Type Height:list[int]: Rtype:int"""                if  notHeightorLen (height) < 3:            return0 S=[] Water=0 Maxbotwater=0 I=0 whileI <len (height):if  notSorHeight[i] <= height[s[-1]]: s.append (i) I+ = 1Else: Bot=Height[s.pop ()] Maxbotwater= 0if  notSElse(min (height[s[-1]],height[i])-Bot) * (i-s[-1]-1) Water+=MaxbotwaterreturnWater

The specific explanation refers to the leetcode discuss. The problem is not well understood, it only needs to be looked at.

Trapping Rain Water

Related Article

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.