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 .
Public classSolution { Public intTrapint[] height) { if(Height = =NULL|| Height.length = = 0 | | Height.length = = 1)return0; //The amount of water per I can depend on the minimum value of the left and right height vol[i]=min (left[i],right[i])-height[i]//iterate twice to get the highest right panel and the top left panel left[],right[] in the Traverse once calculation. intLeft[] =New int[Height.length]; intRight[] =New int[Height.length]; //get the right top plate for each indexLeft[0] = height[0]; intMAXL = left[0]; for(inti = 1; i < height.length; i++) {Left[i]=Math.max (MAXL, height[i]); Maxl=Math.max (MAXL, height[i]); } //get the left top plate for each indexRIGHT[HEIGHT.LENGTH-1] = height[height.length-1]; intMAXR = height[height.length-1]; for(inti = height.length-2; i>=0; i--) {Right[i]=Math.max (MAXR, height[i]); MAXR=Math.max (MAXR, height[i]); } intvol = 0; for(inti = 1; i < height.length-1; i++){ intbit = Math.min (Left[i], right[i])-Height[i]; if(Bit > 0) Vol + =bit; } returnVol; }}
Trapping Rain Water