class Solution {
public int trap(int[] height) {
int left = 0, right = height.length - 1;
int ans = 0;
while (left < right) {
int t = Math.min(height[left], height[right]);
while (height[left] <= t && left < right) {
ans += t - height[left];
left++;
}
while (height[right] <= t && left < right) {
ans += t - height[right];
right--;
}
}
return ans;
}
}