```
public class Solution {
public int trap(int[] height) {
if(height.length == 0)
return 0;
Stack<Integer> stack = new Stack<Integer>(); // store index, min stack of height[i]
int vol = 0;
for(int i = 0; i < height.length; i++) {
int cur = height[i];
if(stack.empty() || height[stack.peek()] > cur) {
stack.push(i);
} else {
while(!stack.empty() && height[stack.peek()] < cur) {
int index = stack.pop();
if(!stack.empty()) {
int j = stack.peek();
int edge = Math.min(cur, height[j]);
vol += (i - j - 1) * (edge - height[index]);
}
}
stack.push(i);
}
}
return vol;
}
```

}