class Solution {
public:
int trap(vector<int>& height) {
int len = height.size();
int ret = 0;
if(len<=2) return ret;
vector<int> leftMax(len,0);
vector<int> rightMax(len,0);
int leftMa = leftMax[0] = height[0];
for(int i =1;i<len;i++)
{
if(height[i]>leftMa) leftMa = height[i];
leftMax[i] = leftMa;
}
int rightMa = rightMax[len-1] = height[len-1];
for(int j = len-2;j>=0;j–)
{
if(height[j]>rightMa) rightMa = height[j];
rightMax[j] = rightMa;
}
for(int i = 0;i<len;i++)
{
int m = min(leftMax[i],rightMax[i]);
int w = m-height[i];
if(w<0) w=0;
ret +=w;
}
return ret;
}
};