From 6edc009765a3f42428649431abdf0fa6c27d94af Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 26 Aug 2024 08:37:00 +0200 Subject: [PATCH] test: `LongestAlternatingSubsequenceTest` (#5399) --- .../LongestAlternatingSubsequence.java | 80 +++++++++---------- .../LongestAlternatingSubsequenceTest.java | 22 +++++ 2 files changed, 62 insertions(+), 40 deletions(-) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java index d6f9b2ac..08039b85 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java @@ -1,38 +1,48 @@ package com.thealgorithms.dynamicprogramming; -/* - - * Problem Statement: - - * Find Longest Alternating Subsequence - - * A sequence {x1, x2, .. xn} is alternating sequence if its elements satisfy one of the following - relations : - - x1 < x2 > x3 < x4 > x5 < …. xn or - x1 > x2 < x3 > x4 < x5 > …. xn +/** + * Class for finding the length of the longest alternating subsequence in an array. + * + *

An alternating sequence is a sequence of numbers where the elements alternate + * between increasing and decreasing. Specifically, a sequence is alternating if its elements + * satisfy one of the following relations: + * + *

+ * + *

This class provides a method to compute the length of the longest such subsequence + * from a given array of integers. */ public final class LongestAlternatingSubsequence { private LongestAlternatingSubsequence() { } - /* Function to return longest alternating subsequence length*/ + /** + * Finds the length of the longest alternating subsequence in the given array. + * + * @param arr an array of integers where the longest alternating subsequence is to be found + * @param n the length of the array {@code arr} + * @return the length of the longest alternating subsequence + * + *

The method uses dynamic programming to solve the problem. It maintains a 2D array + * {@code las} where: + *

+ * + *

The method iterates through the array and updates the {@code las} array based on + * whether the current element is greater or smaller than the previous elements. + * The result is the maximum value found in the {@code las} array. + */ static int alternatingLength(int[] arr, int n) { - /* - - las[i][0] = Length of the longest - alternating subsequence ending at - index i and last element is - greater than its previous element - - las[i][1] = Length of the longest - alternating subsequence ending at - index i and last element is - smaller than its previous - element - - */ int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence + // Initialize the dp array for (int i = 0; i < n; i++) { las[i][0] = 1; las[i][1] = 1; @@ -40,34 +50,24 @@ public final class LongestAlternatingSubsequence { int result = 1; // Initialize result - /* Compute values in bottom up manner */ + // Compute values in a bottom-up manner for (int i = 1; i < n; i++) { - /* Consider all elements as previous of arr[i]*/ for (int j = 0; j < i; j++) { - /* If arr[i] is greater, then check with las[j][1] */ + // If arr[i] is greater than arr[j], update las[i][0] if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) { las[i][0] = las[j][1] + 1; } - /* If arr[i] is smaller, then check with las[j][0]*/ + // If arr[i] is smaller than arr[j], update las[i][1] if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) { las[i][1] = las[j][0] + 1; } } - /* Pick maximum of both values at index i */ - if (result < Math.max(las[i][0], las[i][1])) { - result = Math.max(las[i][0], las[i][1]); - } + // Pick the maximum of both values at index i + result = Math.max(result, Math.max(las[i][0], las[i][1])); } return result; } - - public static void main(String[] args) { - int[] arr = {10, 22, 9, 33, 49, 50, 31, 60}; - int n = arr.length; - System.out.println("Length of Longest " - + "alternating subsequence is " + alternatingLength(arr, n)); - } } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java new file mode 100644 index 00000000..3de1114a --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java @@ -0,0 +1,22 @@ +package com.thealgorithms.dynamicprogramming; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class LongestAlternatingSubsequenceTest { + + @ParameterizedTest + @MethodSource("provideTestCases") + void testAlternatingLength(int[] arr, int expected) { + assertEquals(expected, LongestAlternatingSubsequence.alternatingLength(arr, arr.length)); + } + + private static Stream provideTestCases() { + return Stream.of(Arguments.of(new int[] {1}, 1), Arguments.of(new int[] {1, 2}, 2), Arguments.of(new int[] {2, 1}, 2), Arguments.of(new int[] {1, 3, 2, 4, 3, 5}, 6), Arguments.of(new int[] {1, 2, 3, 4, 5}, 2), Arguments.of(new int[] {5, 4, 3, 2, 1}, 2), + Arguments.of(new int[] {10, 22, 9, 33, 49, 50, 31, 60}, 6), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2)); + } +}