public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<>();
int maxArea = 0, area = 0, curHeight = 0;
for (int i = 0; i <= heights.length; i++) {
curHeight = i == heights.length ? -1 : heights[i];
while (!stack.isEmpty() && heights[stack.peek()] >= curHeight) {
area = heights[index] * (stack.isEmpty() ? i : i - stack.peek() - 1);
maxArea = Math.max(maxArea, area);