Leetcode [42] (Java): Trapping Rain Water

Source: Internet
Author: User

Topic : Receiving Rainwater

Difficulty: Hard

topic content :

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.


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!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]output:6

translation : The simple translation is to give the number of blocks in each block, and finally find out how much rain they can catch when they are put together.

My idea : this question and the 11th question is very similar: Container with the most water, but the 11 question is the vertical line, here is the square, moreover that is seeks the water which the most, here is asks can catch all water, but takes the water the thought may refer, is uses the double pointer , moving in the same direction. Each time you move the shorter side of the one.

However, I will not do this because I want to subtract the volume of the square body.

Answer code :

1      Public intTrapint[] height) {2         if(Height.length = = 0)return0;3         intleft = 0;4         intright = Height.length-1;5         intArea = 0, Leftheight = height[0], rightheight = height[height.length-1];6          while(Left <Right ) {7             if(Height[left] <Height[right]) {8left++;9Leftheight =Math.max (Leftheight, Height[left]);TenArea + = Leftheight-Height[left]; One             } A             Else{ -right--; -Rightheight =Math.max (Rightheight, Height[right]); theArea + = Rightheight-Height[right]; -             } -         } -         returnArea ; +}

Complexity of the answer: O (N)

The answer: Sure enough, is the use of a double pointer, each move the relatively short side, and then take the highest height at this time.

And then!

If the current height is higher than the highest height, then the amount of water storage must be added to this difference between.

(because it is shorter than the height of the upper side, so this difference in water is bound to catch)

For example: ("" instead of block, ___ stands for 0 blocks)

【】

【】      【】

【】_________【】【】

A B C

First of all because a is higher than C, so move C over the pointer right,right move to point B, and then take High[right] (b) with the highest height before the C comparison, get or C high, and because this side is short sooner or later than this high, so at this time the water storage should be added (C-b).

And if the current is the highest height after the move, then the water storage is not increased.

So the last time after each move, the amount of water should be added (this side maximum height-current height), corresponding code: Area + = Leftheight-height[left];

Leetcode [42] (Java): 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.