Trapping Rain Water

Source: Internet
Author: User

https://leetcode.com/problems/trapping-rain-water/

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!

Problem Solving Ideas:

This question I once AC believe, some people say leetcode in fact the biggest charm is to see pending-judging-accept, Green Word a flash, refreshed and cool.

The problem before the focus on the two pointers the topic of the time to try to do, the topic after the thought of a while, no idea to give up. Today before doing Candy this problem when the netizen said that this problem and it is very similar, are scanned from both ends two times, only to start thinking how to do.

Candy is also my own write out, and have a hint, thought can ease point, can still think for a long time. Influenced by the Candy thought, we began to consider how to use the relationship between two values and the current value to determine the current height of the water, and think about the solution. Then reconsider how the brain generally considers the problem.

Looking at the above figure, our human eyes are always at the maximum width possible, determine the left and right two boundaries, within their values, are smaller than these two values. So the interior is a pit, the height of the pit is the smaller of the two values. So, the sensitive thought, this process seems to be able to use the above said from left to right and from right to left to scan two times to solve.

First, scan from left to right to record the maximum value Topsofar currently encountered. If the Topsofar is larger than the current value a[i], it means that he may be the current water height top[i]. Otherwise, use the current a[i] to update the Topsofar and current water height top[i].

Why is that possible? So under what circumstances is it, under what circumstances not? Looking at the picture, the answer is that if there is a A[j "(J > I) at the right of the current I, which is larger than I topsofar, the current Top[i] is established. Isn't that the right-to-left scan again?

Then the second time, from right to left, keeps track of the maximum value Topsofar currently encountered. Only Topsofar is larger than top[i], top[i] is effective. Because it represents a higher pillar on the right side of I. than in the middle of the big blue pit. If Topsofar is smaller than top[i], use Topsofar to update top[i]. That is, with the above thinking in turn update top[i], because the above has been scanned, at this time I left there must be a higher than I topsofar column, so the current Topsofar must be here the water height, than the last piece of blue.

 Public classSolution { Public intTrapint[] A) {int[] top =New int[A.length]; if(A.length = = 0 | | A.length = = 1){            return0; }        intTopsofar = a[0]; //from left to right, Topsofar is bigger than current, the current top is Topsofar         for(inti = 0; i < a.length; i++){            if(A[i] <=Topsofar) {Top[i]=Topsofar; }Else{Topsofar=A[i]; Top[i]=A[i]; }        }                //from right to left, only topsofar than Top[i] big, top[i] only effective, otherwise top[i] is TopsofarTopsofar = a[a.length-1];  for(inti = a.length-1; I >= 0; i--){            if(A[i] >Topsofar) {Topsofar=A[i]; }            if(Topsofar <Top[i]) {Top[i]=Topsofar; }        }                intArea = 0;  for(inti = 0; i < a.length-1; i++) { area+ = Top[i]-A[i]; }        returnArea ; }}

This is inscribed out, then look, or to understand to be clear, it should be said in the same kind of topic is more test thinking.

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.