84 Largest Rectangle in Histogram
Last updated
Last updated
class Solution {
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) {
int index = stack.pop();
area = heights[index] * (stack.isEmpty() ? i : i - stack.peek() - 1);
maxArea = Math.max(maxArea, area);
}
stack.push(i);
}
return maxArea;
}
}