forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRectangle.java
More file actions
52 lines (45 loc) · 1.6 KB
/
LargestRectangle.java
File metadata and controls
52 lines (45 loc) · 1.6 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.thealgorithms.stacks;
import java.util.Stack;
/**
* Utility class to calculate the largest rectangle area in a histogram.
* Each bar's width is assumed to be 1 unit.
*
* <p>This implementation uses a monotonic stack to efficiently calculate
* the area of the largest rectangle that can be formed from the histogram bars.</p>
*
* <p>Example usage:
* <pre>{@code
* int[] heights = {2, 1, 5, 6, 2, 3};
* String area = LargestRectangle.largestRectangleHistogram(heights);
* // area is "10"
* }</pre>
*/
public final class LargestRectangle {
private LargestRectangle() {
}
/**
* Calculates the largest rectangle area in the given histogram.
*
* @param heights an array of non-negative integers representing bar heights
* @return the largest rectangle area as a {@link String}
*/
public static String largestRectangleHistogram(int[] heights) {
int maxArea = 0;
Stack<int[]> stack = new Stack<>();
for (int i = 0; i < heights.length; i++) {
int start = i;
while (!stack.isEmpty() && stack.peek()[1] > heights[i]) {
int[] popped = stack.pop();
maxArea = Math.max(maxArea, popped[1] * (i - popped[0]));
start = popped[0];
}
stack.push(new int[] {start, heights[i]});
}
int totalLength = heights.length;
while (!stack.isEmpty()) {
int[] remaining = stack.pop();
maxArea = Math.max(maxArea, remaining[1] * (totalLength - remaining[0]));
}
return Integer.toString(maxArea);
}
}