public class Solution {
public int largestRectangleArea(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
Stack<Integer> s = new Stack<Integer>();
int length = height.length;
int max = 0;
int i = 0;
while (i < length) {
if (s.isEmpty() || height[i] >= height[s.peek()]) {
s.push(i);
i++;
} else {
int loc = s.pop();
int high = height[loc];
int width = s.isEmpty() ? i : i - s.peek() - 1;
max = Math.max(width * high, max);
}
}
while (!s.isEmpty()) {
int loc = s.pop();
int high = height[loc];
int width = s.isEmpty() ? i : i - s.peek() -1 ;
max = Math.max(width * high, max);
}
return max;
}
}