/
LargestRectangleInHistogram.java
36 lines (35 loc) · 1.1 KB
/
LargestRectangleInHistogram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class LargestRectangleInHistogram {
public static int largestRectangleArea(int[] height) {
/* use monotonous stack */
if (height.length == 0)
return 0;
int[] stack = new int[height.length + 1];
int top = 0;
stack[top] = -1;
int result = 0;
for (int i = 0; i <= height.length; i++) {
int h;
if (i == height.length) {
h = -1;
} else {
h = height[i];
}
while (top != 0 && height[stack[top]] > h) {
int temp = height[stack[top]] * (i - 1 - stack[top - 1]);
if (temp > result) {
result = temp;
}
top--;
}
top++;
stack[top] = i;
}
return result;
}
public static void main(String[] args) {
int[] height = {2,1,5,6,2,3};
System.out.println(largestRectangleArea(height));
System.out.println(largestRectangleArea(new int[]{}));
System.out.println(largestRectangleArea(new int[]{0}));
}
}