2
public int largestRectangleArea(int[] heights) {3
Stack<Integer> stack1 = new Stack<>();4
Stack<Integer> stack2 = new Stack<>();5
int n = heights.length;6
int[] left = new int[n];7
int[] right = new int[n];8
int[] width = new int[n];10
for (int i = 0; i < n; i++) {11
while (!stack1.isEmpty() && heights[stack1.peek()] >= heights[i]) stack1.pop();12
if (!stack1.isEmpty()) left[i] = stack1.peek();17
for (int i = n - 1; i >= 0; i--) {18
while (!stack2.isEmpty() && heights[stack2.peek()] >= heights[i]) stack2.pop();19
if (!stack2.isEmpty()) right[i] = stack2.peek();24
for (int i = 0; i < n; i++) {25
width[i] = right[i] - left[i] - 1;28
for (int i = 0; i < n; i++) {29
mxArea = Math.max(mxArea, width[i] * heights[i]);