class Solution {

public int largestRectangleArea(int[] heights) {

Stack<Integer> stack = new Stack<Integer>();

int maxArea = 0;

```
for (int i = 0; i <= heights.length; i++) {
int h = i == heights.length ? 0 : heights[i];
while (!stack.isEmpty() && heights[stack.peek()] > h) {
int indexPopout = stack.pop();
maxArea = stack.isEmpty() ? Math.max(maxArea, i * heights[indexPopout])
: Math.max(maxArea, (i - stack.peek() - 1) * heights[indexPopout]);
}
stack.push(i);
}
return maxArea;
}
```

}