Trapping Rain Water
Problem:
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.
Ideas:
Array of special, get the highest point value, left and right side of the test separately
My Code:
Public classSolution { Public intTrapint[] A) {if(A = =NULL|| A.length = = 0)return0; intMaxindex =-1; intMaxValue =Integer.min_value; for(inti = 0; i < a.length; i++) { if(MaxValue <A[i]) {Maxindex=i; MaxValue=A[i]; } } intMaxres = 0; intCurmax = a[0]; for(inti = 1; I <= Maxindex; i++) { if(A[i] <Curmax) {Maxres+ = Curmax-A[i]; } Else{Curmax=A[i]; }} Curmax= A[a.length-1]; for(inti = a.length-1; I >= Maxindex; i--) { if(A[i] <Curmax) {Maxres+ = Curmax-A[i]; } Else{Curmax=A[i]; } } returnMaxres; }}View Code
The Learning Place:
Grasp the characteristics of the highest point of the array
Trapping Rain Water