Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
public class Solution {
public int trap(int[] height) {
int pre, rs = 0, Pos = 0, length = height.length, index;
for(index = 1; index<length; index++) {
if(height[index]>=height[Pos]) Pos = index;
}
for(pre = index = 0; index<Pos; index++) {
if(height[index]<pre) rs += (pre-height[index]);
else pre = height[index];
}
for(pre = 0, index = length-1; index>Pos; index--) {
if(height[index]<pre) rs += (pre-height[index]);
else pre = height[index];
}
return rs;
}
}