int trap(int* height, int heightSize) {
int sum = 0;
int l = 0;
int r = heightSize - 1;
int l_top = height[0];
int r_top = height[heightSize - 1];
while(l < r){
if(height[l] < height[r]){
if(l_top >= height[l]) sum += (l_top - height[l]);
else l_top = height[l];
++l;
}
else{
if(r_top >= height[r]) sum += (r_top - height[r]);
else r_top = height[r];
--r;
}
}
return sum;
}
int sum = 0;
int l = 0;
int r = heightSize - 1;
int l_top = height[0];
int r_top = height[heightSize - 1];
while(l < r){
if(height[l] < height[r]){
if(l_top >= height[l]) sum += (l_top - height[l]);
else l_top = height[l];
++l;
}
else{
if(r_top >= height[r]) sum += (r_top - height[r]);
else r_top = height[r];
--r;
}
}
return sum;
}