For explanation, please see http://www.geeksforgeeks.org/largest-rectangle-under-histogram/

```
public class Solution {
public int largestRectangleArea(int[] height) {
int len = height.length;
Stack<Integer> s = new Stack<Integer>();
int maxArea = 0;
for(int i = 0; i <= len; i++){
int h = (i == len ? 0 : height[i]);
if(s.isEmpty() || h >= height[s.peek()]){
s.push(i);
}else{
int tp = s.pop();
maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
i--;
}
}
return maxArea;
}
}
```

OP's Note: Two years later I need to interview again. I came to this problem and I couldn't understand this solution. After reading the explanation through the link above, I finally figured this out again.

Two key points that I found helpful while understanding the solution:

- Do push all heights including 0 height.
`i - 1 - s.peek()`

uses the starting index where`height[s.peek() + 1] >= height[tp]`

, because the index on top of the stack right now is the first index left of`tp`

with height smaller than tp's height.