Trapping Rain Water

Source: Internet
Author: User

A look at this problem is too simple to write your own, the code is a=height in order to be able to directly use other people's code.

Transferred from: http://m.blog.csdn.net/blog/nerv3x3/37339357

Ideas:

Consider a series as a pile of high and low water storage walls, the height of the wall, the maximum amount of water storage. In fact, the picture in the topic is already at a glance, without too much text to explain.
For an X-point, thinking determines that its amount of light is determined by the smallest of the tallest walls on the left and right sides of the point. For example, for a=[0,1,0,2,1,0,1,3,2,1,2,1] This example, a[4]=1 this point, the top wall that appears on the left is a[3]=2, and the tallest wall that appears on the right is a[7]=3, so the smallest of the two is a[3]=2, so a[4] The water storage height is a[3]-a[4]=1, multiplied by the bottom 1, that is, a[4] 1.
It is obviously unreasonable to look for the minimum value individually for each point so that the time complexity is O (n*n). Request an array of the same length as the original array max_heights is used to record the maximum amount of each point, first from left to right through a, record the maximum value that is currently encountered, and then fill in into max_heights. Then the right-to-left traversal of a, also records the maximum value that is currently encountered, and the corresponding position in the Max_heights value is compared, if the corresponding value in the max_heights is smaller than the value in the max_heights is replaced. This way, you can use 2 times the time complexity of O (n) traversal to find each point of water storage, and then traverse Max_heights, if the corresponding value in a is larger than the sum of the difference, the last is the total amount of water. That is, if the value of a is large in max_heights, it means that the point cannot be water-storing.
The spatial complexity is O (n) and the time complexity is O (n).

classSolution:#@param {integer[]} height    #@return {integer}    defTrap (self, height): A=Height len_a=Len (A)if1 = =len_a:return0 max_heights= [0] *len_a Left_max=0 forIinchRange (0, len_a):ifA[i] >Left_max:left_max=A[i] Max_heights[i]=Left_max Right_max=0 forIinchRange (Len_a-1,-1,-1):            ifA[i] >Right_max:right_max=A[i]ifRight_max <Max_heights[i]: max_heights[i]=Right_max Result=0 forIinchRange (0, len_a):ifMax_heights[i] >A[i]: result+ = (Max_heights[i]-A[i])returnresult

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.