From 05fc0c1448028e797a11d8d1fa772d1ef1678ccb Mon Sep 17 00:00:00 2001 From: mvouzkis Date: Mon, 5 Feb 2024 14:05:48 +0200 Subject: [PATCH 1/2] I added some descriptive javadoc and commments throughout the CalculateMaxofMin.java file --- .../stacks/CalculateMaxOfMin.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java b/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java index 399b9efdc49b..8a8c5874a558 100644 --- a/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java +++ b/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java @@ -4,15 +4,27 @@ */ /** - * Program description - Given an integer array. The task is to find the maximum of the minimum of - * the array - */ + * The {@code CalculateMaxOfMin} class provides a method to calculate the maximum + * value among the minimum values of a given array. +*/ package com.thealgorithms.stacks; -import java.util.*; +import java.util.Arrays; public class CalculateMaxOfMin { + /** + * Calculates the maximum value among the minimum values of all subarrays. + * + * @param a The input array of integers. + * @return The maximum value among the minimum values of all subarrays. + * + * The algorithm works by considering subarrays of increasing lengths using + * a sliding window approach. It calculates the minimum value for each subarray + * and then finds the maximum value among those minimum values. This process + * is repeated for different subarray lengths until the entire array is considered. + * The result is the maximum of the minimum values for all subarrays. + */ public static int calculateMaxOfMin(int[] a) { int n = a.length; int[] ans = new int[n]; @@ -20,23 +32,29 @@ public static int calculateMaxOfMin(int[] a) { Arrays.sort(arr2); int maxNum = arr2[arr2.length - 1]; ans[0] = maxNum; + + // Iterate through the array to calculate the maximum of the minimum values int index = 1; while (index != ans.length) { + + // Array to store minimum values for subarrays of the specified length int[] minimums = new int[n - index]; + + // Calculate minimum values for each subarray for (int i = 0; i < n - index; i++) { int[] windowArray = Arrays.copyOfRange(a, i, i + index + 1); Arrays.sort(windowArray); int minNum = windowArray[0]; minimums[i] = minNum; } + + // Sort the minimums array and set the corresponding value in the result array Arrays.sort(minimums); ans[index] = minimums[minimums.length - 1]; + index += 1; } return ans[0]; } } -/** - * Given an integer array. The task is to find the maximum of the minimum of the - * given array - */ + From b2f0f168e07df0b236c8c9eaf20cf3aae142d94c Mon Sep 17 00:00:00 2001 From: vil02 Date: Mon, 5 Feb 2024 21:32:58 +0100 Subject: [PATCH 2/2] fix: use proper formatting --- .../java/com/thealgorithms/stacks/CalculateMaxOfMin.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java b/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java index 8a8c5874a558..6ca7dec3cdcb 100644 --- a/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java +++ b/src/main/java/com/thealgorithms/stacks/CalculateMaxOfMin.java @@ -6,19 +6,19 @@ /** * The {@code CalculateMaxOfMin} class provides a method to calculate the maximum * value among the minimum values of a given array. -*/ + */ package com.thealgorithms.stacks; import java.util.Arrays; public class CalculateMaxOfMin { - /** + /** * Calculates the maximum value among the minimum values of all subarrays. * * @param a The input array of integers. * @return The maximum value among the minimum values of all subarrays. - * + * * The algorithm works by considering subarrays of increasing lengths using * a sliding window approach. It calculates the minimum value for each subarray * and then finds the maximum value among those minimum values. This process @@ -57,4 +57,3 @@ public static int calculateMaxOfMin(int[] a) { return ans[0]; } } -