```
public int Trap(int[] height) {
var res = 0;
int left = 0, right = height.Length - 1;
int maxLeft = 0, maxright = 0;
while (left < right)
{
if (height[left] < height[right])
{
if (height[left] > maxLeft) maxLeft = height[left];
else res += maxLeft - height[left];
left++;
}
else
{
if (height[right] > maxright) maxright = height[right];
else res += maxright - height[right];
right--;
}
}
return res;
}
```