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 .
Reference:
http://blog.csdn.net/doc_sgl/article/details/12307171
1 Public classSolution {2 Public intTrapint[] A) {3 intWatersum = 0;4 intLeftmaxheight[] =New int[a.length];5 intRightmaxheight[] =New int[a.length];6 7 intMaxHeight = 0;8 for(inti = 0; i < a.length; i++){9Leftmaxheight[i] =MaxHeight;TenMaxHeight = maxheight > A[i]?Maxheight:a[i]; One}// for A -MaxHeight = 0; - for(inti = a.length-1; I >= 0; i--){ theRightmaxheight[i] =MaxHeight; -MaxHeight = maxheight > A[i]?Maxheight:a[i]; -}// for - + for(inti = 0; i < a.length; i++){ - intMinHeight =math.min (Leftmaxheight[i], rightmaxheight[i]); + intElementsum = MinHeight-A[i]; A if(Elementsum > 0) atWatersum + =elementsum; -}// for - - returnwatersum; - } - in}
Trapping Rain Water