Thought Process: The water can only be contained as tall as the shorter side of wall. So We can scan the array from both right and left, and maintain a mininum height. Add height difference between either left height and right height with the mininum height to the area.
class Solution {
public:
int trap(vector<int>& height) {
int n = height.size(), l = 0, r = n-1, water = 0, minHeight = 0;
while(l<r){
while(l<r && height[l]<=minHeight)
water += minHeight-height[l++];
while(l<r && height[r]<=minHeight)
water += minHeight-height[r--];
minHeight = min(height[l], height[r]);
}
return water;
}
};