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