Largest Rectangle in Histogram
Last updated
Last updated
class Solution {
public int largestRectangleArea(int[] heights) {
int res = 0;
if (heights == null || heights.length == 0) {
return res;
}
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i <= heights.length; i++) {
int h = (i == heights.length) ? -1 : heights[i];
while (!stack.isEmpty() && h <= heights[stack.peek()]) {
int height = heights[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
System.out.println(height * w);
res = Math.max(res, height * w);
}
stack.push(i);
}
return res;
}
}