-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathLargestRectangle.java
38 lines (33 loc) · 1.08 KB
/
LargestRectangle.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
37
38
package com.thealgorithms.stacks;
import java.util.Stack;
/**
*
* @author mohd rameez github.com/rameez471
*/
public final class LargestRectangle {
private LargestRectangle() {
}
public static String largestRectangleHistogram(int[] heights) {
int n = heights.length;
int maxArea = 0;
Stack<int[]> st = new Stack<>();
for (int i = 0; i < n; i++) {
int start = i;
while (!st.isEmpty() && st.peek()[1] > heights[i]) {
int[] tmp = st.pop();
maxArea = Math.max(maxArea, tmp[1] * (i - tmp[0]));
start = tmp[0];
}
st.push(new int[] {start, heights[i]});
}
while (!st.isEmpty()) {
int[] tmp = st.pop();
maxArea = Math.max(maxArea, tmp[1] * (n - tmp[0]));
}
return Integer.toString(maxArea);
}
public static void main(String[] args) {
assert largestRectangleHistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
assert largestRectangleHistogram(new int[] {2, 4}).equals("4");
}
}