Skip to content

Commit 37a174a

Browse files
Merge branch 'master' into refactor/isomorphic_strings
2 parents 42b4e8f + 2ccc156 commit 37a174a

File tree

4 files changed

+66
-120
lines changed

4 files changed

+66
-120
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
}
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.thealgorithms.strings;
22

33
/**
4+
* Utility class for checking if a string's characters are in alphabetical order.
5+
* <p>
46
* Alphabetical order is a system whereby character strings are placed in order
57
* based on the position of the characters in the conventional ordering of an
6-
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
8+
* alphabet.
9+
* <p>
10+
* Reference: <a href="https://en.wikipedia.org/wiki/Alphabetical_order">Wikipedia: Alphabetical Order</a>
711
*/
8-
final class Alphabetical {
12+
public final class Alphabetical {
913
private Alphabetical() {
1014
}
1115

12-
public static void main(String[] args) {
13-
assert !isAlphabetical("123abc");
14-
assert isAlphabetical("aBC");
15-
assert isAlphabetical("abc");
16-
assert !isAlphabetical("xyzabc");
17-
assert isAlphabetical("abcxyz");
18-
}
19-
2016
/**
21-
* Check if a string is alphabetical order or not
17+
* Checks whether the characters in the given string are in alphabetical order.
18+
* Non-letter characters will cause the check to fail.
2219
*
23-
* @param s a string
24-
* @return {@code true} if given string is alphabetical order, otherwise
25-
* {@code false}
20+
* @param s the input string
21+
* @return {@code true} if all characters are in alphabetical order (case-insensitive), otherwise {@code false}
2622
*/
2723
public static boolean isAlphabetical(String s) {
2824
s = s.toLowerCase();
2925
for (int i = 0; i < s.length() - 1; ++i) {
30-
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) {
26+
if (!Character.isLetter(s.charAt(i)) || s.charAt(i) > s.charAt(i + 1)) {
3127
return false;
3228
}
3329
}
34-
return true;
30+
return !s.isEmpty() && Character.isLetter(s.charAt(s.length() - 1));
3531
}
3632
}

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
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
54

6-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
77

88
public class AlphabeticalTest {
99

10-
@Test
11-
public void isAlphabetical() {
12-
// expected to be true
13-
String input1 = "abcdefghijklmno";
14-
String input2 = "abcdxxxyzzzz";
15-
String input3 = "fpw";
16-
17-
// expected to be false
18-
String input4 = "123a";
19-
String input5 = "abcABC";
20-
String input6 = "abcdefghikjlmno";
21-
22-
assertTrue(Alphabetical.isAlphabetical(input1));
23-
assertTrue(Alphabetical.isAlphabetical(input2));
24-
assertTrue(Alphabetical.isAlphabetical(input3));
25-
26-
assertFalse(Alphabetical.isAlphabetical(input4));
27-
assertFalse(Alphabetical.isAlphabetical(input5));
28-
assertFalse(Alphabetical.isAlphabetical(input6));
10+
@ParameterizedTest(name = "\"{0}\" → Expected: {1}")
11+
@CsvSource({"'abcdefghijklmno', true", "'abcdxxxyzzzz', true", "'123a', false", "'abcABC', false", "'abcdefghikjlmno', false", "'aBC', true", "'abc', true", "'xyzabc', false", "'abcxyz', true", "'', false", "'1', false"})
12+
void testIsAlphabetical(String input, boolean expected) {
13+
assertEquals(expected, Alphabetical.isAlphabetical(input));
2914
}
3015
}

0 commit comments

Comments
 (0)