class Solution {
public int trap(int[] height) {
int total = 0;
int left = 0, right = height.length - 1;
int l_max = 0, r_max = 0;
while (left < right) {
l_max = Math.max(l_max, height[left]);
r_max = Math.max(r_max, height[right]);
if (l_max < r_max) {
total += l_max - height[left];
left++;
} else {
total += r_max - height[right];
right--;
}
}
return total;
}
}