[Leedcode 42] 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!

 Public classSolution {//dynamic programming, constructed of two arrays of Leftmax and Rightmax, representing the highest pillar on the left and the tallest pillar on the right, respectively .//Leftmax[i]=math.max (Leftmax[i-1],height[i-1], and the right side, notice Leftmax[i] represents the largest column on the left side of I, excluding I//Therefore, each node can fill the capacity of water through Math.min (Rightmax[i],leftmax[i])-height[i], to fill the total capacity of water, only need to traverse the array. //Note that the capacity of each node to fill the water can be reused before the Rightmax array. //the time complexity of this solution is O (n), and the spatial complexity is also O (n)     Public intTrapint[] height) {       /*int res=0;        int len=height.length;         if (len<=2) return res;        int leftmax[]=new Int[len];        leftmax[0]=0;        int rightmax[]=new Int[len];        rightmax[len-1]=0;        for (int i=1;i<len;i++) {Leftmax[i]=math.max (leftmax[i-1],height[i-1]);        } for (int i=len-2;i>=0;i--) {Rightmax[i]=math.max (rightmax[i+1],height[i+1]);            } for (int i=0;i<len;i++) {rightmax[i]=math.min (Rightmax[i],leftmax[i])-height[i];        if (rightmax[i]>0) res+=rightmax[i]; } return res;*/                //The time complexity of this solution is O (n), which only needs to be traversed once. Space complexity is O (1)//The idea is to use L and r two pointers to maintain the position on both sides of the water. //when the height of the L is low, the water on the right side is sure to be as high as L, at this time gradually right to move l, with the position of L and right after the height difference (because it can be filled with water ah), until again encountered the same//High or higher position.        Then make the next round of judgment. //Similarly, when the height of R is low, the water on the left side of R must be as high as r, and then move the R to the left, with the difference between R and the left position, until the same high or higher position is encountered. //finally until L and R meet, end.         intRes=0; intL=0; intlen=height.length; intR=len-1;  while(l<R) {            inttemp=math.min (Height[l],height[r]); if(temp==Height[l]) {L++;  while(l<r&&height[l]<=temp) {Res+=temp-Height[l]; L++; }                            }            Else{R--;  while(l<r&&height[r]<=temp) {Res+=temp-Height[r]; R--; }                     }        }        returnRes; }}

[Leedcode] 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.