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. * *
This implementation uses a monotonic stack to efficiently calculate * the area of the largest rectangle that can be formed from the histogram bars.
* *Example usage: *
{@code
* int[] heights = {2, 1, 5, 6, 2, 3};
* String area = LargestRectangle.largestRectangleHistogram(heights);
* // area is "10"
* }
*/
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