leetcode#42 trapping Rain Water

Source: Internet
Author: User

Original title Address

Try a manual simulation to see if there are rules to find.

Assuming that starting at position 0, we have a rectangular bar

Extending to the right, we encounter the second rectangular bar, assuming that the rectangle is shorter than the first one:

We continue to expand to the right until we encounter a rectangular bar that is taller (or equal) than the first rectangular bar:

So obviously, the blue part is the part of the water that adds the part to the final result.

Then, with the newly discovered rectangle bar as the left edge, continue to the right extension ...

By simulating we found that we fixed a left boundary, and then we looked to the right boundary, which is the right boundary? The first rectangular bar that is higher (or equal) than the left border is the right boundary.

This obviously has a problem, such as the following case, the rightmost rectangle is the last rectangle bar, the blue part will not be counted, because the rightmost rectangle bar than the red arrow refers to the rectangle bar is shorter, it is not the right edge.

What do we do? Turn it upside down (right-to-left) and sweep it all over again in the same way.

But there is one more problem, look at the situation below. When the left and right borders (pointed by the Red Arrows) are high, the capacity of this part of the water will be calculated two times (sweep from left to right once, and one at the left).

The solution is also very simple, as long as the left-to-right boundary must be less than the right boundary, and the left boundary must be greater than or equal to the right boundary when sweeping from left to right.

Code:

1 intTrapintA[],intN) {2   intsum =0;3   intACC =0;4   inti =0;5   intj =0;6 7   //swipe from left to right8i =0;9j =1;TenACC =0; One    while(I < n && J <N) { A     if(A[j] <A[i]) -ACC + = A[i]-A[j]; -     if(A[j] >A[i]) { theSum + =acc; -ACC =0; -i =J; -     } +J + +; -   } +  A   //swipe from right to left ati = n-1; -j = N-2; -ACC =0; -    while(I >=0&& J >=0) { -     if(A[j] <A[i]) -ACC + = A[i]-A[j]; in     Else { -Sum + =acc; toACC =0; +i =J; -     } thej--; *   } $ Panax Notoginseng   returnsum; -}

leetcode#42 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.