public class Solution {
//http://fisherlei.blogspot.com/2013/01/leetcode-trapping-rain-water.html 双扫
public int trap(int[] A) {
if (A.length<=2) return 0;
int crtV = 0, re=0, max = A[0];
int [] maxR = new int[A.length];
//int [] maxL = maxR;
int [] maxL = new int[A.length];
maxL[0]=0;
maxR[A.length-1]=0;
for(int i=1;i<A.length-1;i++){ // consider the boundary values
maxL[i] = max;
if(A[i]>max) max = A[i];
}
max = A[A.length-1];
for(int i=A.length-2;i>0;i--){
maxR[i]=max;
crtV = Math.min(maxR[i],maxL[i])-A[i];
if(crtV>0) re=re+crtV;
if(max<A[i]) max=A[i];
}
return re;
}
}
10-17
10-17
10-17