Skip to content

Commit 2ccc156

Browse files
refactor: clean up LargestRectangle and convert tests to parameterized format (#6356)
* refactor: clean up LargestRectangle and convert tests to parameterized format * refactor: fix clang formatting issue * refactor: fix clang formatting issue for test data --------- Co-authored-by: Deniz Altunkapan <[email protected]>
1 parent f325279 commit 2ccc156

File tree

2 files changed

+47
-82
lines changed

2 files changed

+47
-82
lines changed

src/main/java/com/thealgorithms/stacks/LargestRectangle.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,50 @@
33
import java.util.Stack;
44

55
/**
6+
* Utility class to calculate the largest rectangle area in a histogram.
7+
* Each bar's width is assumed to be 1 unit.
68
*
7-
* @author mohd rameez github.com/rameez471
9+
* <p>This implementation uses a monotonic stack to efficiently calculate
10+
* the area of the largest rectangle that can be formed from the histogram bars.</p>
11+
*
12+
* <p>Example usage:
13+
* <pre>{@code
14+
* int[] heights = {2, 1, 5, 6, 2, 3};
15+
* String area = LargestRectangle.largestRectangleHistogram(heights);
16+
* // area is "10"
17+
* }</pre>
818
*/
9-
1019
public final class LargestRectangle {
20+
1121
private LargestRectangle() {
1222
}
1323

24+
/**
25+
* Calculates the largest rectangle area in the given histogram.
26+
*
27+
* @param heights an array of non-negative integers representing bar heights
28+
* @return the largest rectangle area as a {@link String}
29+
*/
1430
public static String largestRectangleHistogram(int[] heights) {
15-
int n = heights.length;
1631
int maxArea = 0;
17-
Stack<int[]> st = new Stack<>();
18-
for (int i = 0; i < n; i++) {
32+
Stack<int[]> stack = new Stack<>();
33+
34+
for (int i = 0; i < heights.length; i++) {
1935
int start = i;
20-
while (!st.isEmpty() && st.peek()[1] > heights[i]) {
21-
int[] tmp = st.pop();
22-
maxArea = Math.max(maxArea, tmp[1] * (i - tmp[0]));
23-
start = tmp[0];
36+
while (!stack.isEmpty() && stack.peek()[1] > heights[i]) {
37+
int[] popped = stack.pop();
38+
maxArea = Math.max(maxArea, popped[1] * (i - popped[0]));
39+
start = popped[0];
2440
}
25-
st.push(new int[] {start, heights[i]});
41+
stack.push(new int[] {start, heights[i]});
2642
}
27-
while (!st.isEmpty()) {
28-
int[] tmp = st.pop();
29-
maxArea = Math.max(maxArea, tmp[1] * (n - tmp[0]));
43+
44+
int totalLength = heights.length;
45+
while (!stack.isEmpty()) {
46+
int[] remaining = stack.pop();
47+
maxArea = Math.max(maxArea, remaining[1] * (totalLength - remaining[0]));
3048
}
31-
return Integer.toString(maxArea);
32-
}
3349

34-
public static void main(String[] args) {
35-
assert largestRectangleHistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
36-
assert largestRectangleHistogram(new int[] {2, 4}).equals("4");
50+
return Integer.toString(maxArea);
3751
}
3852
}

src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,27 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class LargestRectangleTest {
811

9-
@Test
10-
void testLargestRectangleHistogramWithTypicalCases() {
11-
// Typical case with mixed heights
12-
int[] heights = {2, 1, 5, 6, 2, 3};
13-
String expected = "10";
14-
String result = LargestRectangle.largestRectangleHistogram(heights);
15-
assertEquals(expected, result);
16-
17-
// Another typical case with increasing heights
18-
heights = new int[] {2, 4};
19-
expected = "4";
20-
result = LargestRectangle.largestRectangleHistogram(heights);
21-
assertEquals(expected, result);
22-
23-
// Case with multiple bars of the same height
24-
heights = new int[] {4, 4, 4, 4};
25-
expected = "16";
26-
result = LargestRectangle.largestRectangleHistogram(heights);
27-
assertEquals(expected, result);
12+
@ParameterizedTest(name = "Histogram: {0} → Expected area: {1}")
13+
@MethodSource("histogramProvider")
14+
void testLargestRectangleHistogram(int[] heights, String expected) {
15+
assertEquals(expected, LargestRectangle.largestRectangleHistogram(heights));
2816
}
2917

30-
@Test
31-
void testLargestRectangleHistogramWithEdgeCases() {
32-
// Edge case with an empty array
33-
int[] heights = {};
34-
String expected = "0";
35-
String result = LargestRectangle.largestRectangleHistogram(heights);
36-
assertEquals(expected, result);
37-
38-
// Edge case with a single bar
39-
heights = new int[] {5};
40-
expected = "5";
41-
result = LargestRectangle.largestRectangleHistogram(heights);
42-
assertEquals(expected, result);
43-
44-
// Edge case with all bars of height 0
45-
heights = new int[] {0, 0, 0};
46-
expected = "0";
47-
result = LargestRectangle.largestRectangleHistogram(heights);
48-
assertEquals(expected, result);
18+
static Stream<Arguments> histogramProvider() {
19+
return Stream.of(Arguments.of(new int[] {2, 1, 5, 6, 2, 3}, "10"), Arguments.of(new int[] {2, 4}, "4"), Arguments.of(new int[] {4, 4, 4, 4}, "16"), Arguments.of(new int[] {}, "0"), Arguments.of(new int[] {5}, "5"), Arguments.of(new int[] {0, 0, 0}, "0"),
20+
Arguments.of(new int[] {6, 2, 5, 4, 5, 1, 6}, "12"), Arguments.of(new int[] {2, 1, 5, 6, 2, 3, 1}, "10"), Arguments.of(createLargeArray(10000, 1), "10000"));
4921
}
5022

51-
@Test
52-
void testLargestRectangleHistogramWithLargeInput() {
53-
// Large input case
54-
int[] heights = new int[10000];
55-
for (int i = 0; i < heights.length; i++) {
56-
heights[i] = 1;
57-
}
58-
String expected = "10000";
59-
String result = LargestRectangle.largestRectangleHistogram(heights);
60-
assertEquals(expected, result);
61-
}
62-
63-
@Test
64-
void testLargestRectangleHistogramWithComplexCases() {
65-
// Complex case with a mix of heights
66-
int[] heights = {6, 2, 5, 4, 5, 1, 6};
67-
String expected = "12";
68-
String result = LargestRectangle.largestRectangleHistogram(heights);
69-
assertEquals(expected, result);
70-
71-
// Case with a peak in the middle
72-
heights = new int[] {2, 1, 5, 6, 2, 3, 1};
73-
expected = "10";
74-
result = LargestRectangle.largestRectangleHistogram(heights);
75-
assertEquals(expected, result);
23+
private static int[] createLargeArray(int size, int value) {
24+
int[] arr = new int[size];
25+
java.util.Arrays.fill(arr, value);
26+
return arr;
7627
}
7728
}

0 commit comments

Comments
 (0)