0084. Largest Rectangle in Histogram
Last updated
Last updated
**Input:** heights = [2,1,5,6,2,3]
**Output:** 10
**Explanation:** The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.**Input:** heights = [2,4]
**Output:** 4class Solution {
public int largestRectangleArea(int[] heights) {
// edge cases
if (heights.length == 0) return 0;
int max = 0;
// stack iterate
Stack<Integer> stack = new Stack<Integer>();
stack.push(-1);
for (int i = 0; i <= heights.length; i++) {
int currh = i == heights.length ? 0 : heights[i];
while (stack.peek() != -1 && currh < heights[stack.peek()]) {
int h = heights[stack.pop()];
int w = i - stack.peek() - 1;
max = Math.max(max, h * w);
}
stack.push(i);
}
return max;
}
}
/*
stack
push -1 at first, like #32
push 0 at last, like this one #84
if stack.isEmpty() -> stack.peek() != -1 || heights[i] >= height[stack.peek()], push
while height[i] < height[stack.peek()], h = height[stack.pop()], w = i - stack.peek() - 1
*/class Solution {
public int largestRectangleArea(int[] heights) {
// edge cases
if (heights.length == 0) return 0;
int len = heights.length;
int max = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < heights.length; i++) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
int h = heights[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(max, h * w);
}
stack.push(i);
}
while (!stack.isEmpty()) {
int h = heights[stack.pop()];
int w = stack.isEmpty() ? len : len - stack.peek() - 1;
max = Math.max(max, h * w);
}
return max;
}
}