From bf9d0ed66a867c8197f1cd80c48b6818d7eefee3 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 17 May 2024 11:54:22 +0200 Subject: [PATCH] fix: handle constant inputs in `LongestIncreasingSubsequence::findLISLen' (#5160) fix: handle constant inputs in `LongestIncreasingSubsequence::findLISLen` --- .../LongestIncreasingSubsequence.java | 27 +++------- .../LongestIncreasingSubsequenceTests.java | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java index 9457f0bec..83c319891 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java @@ -1,7 +1,5 @@ package com.thealgorithms.dynamicprogramming; -import java.util.Scanner; - /** * @author Afrizal Fikri (https://github.com/icalF) */ @@ -9,20 +7,6 @@ public final class LongestIncreasingSubsequence { private LongestIncreasingSubsequence() { } - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - - int[] arr = new int[n]; - for (int i = 0; i < n; i++) { - arr[i] = sc.nextInt(); - } - - System.out.println(LIS(arr)); - System.out.println(findLISLen(arr)); - sc.close(); - } - private static int upperBound(int[] ar, int l, int r, int key) { while (l < r - 1) { int m = (l + r) >>> 1; @@ -36,7 +20,7 @@ public final class LongestIncreasingSubsequence { return r; } - private static int LIS(int[] array) { + public static int LIS(int[] array) { int N = array.length; if (N == 0) { return 0; @@ -73,14 +57,17 @@ public final class LongestIncreasingSubsequence { */ // A function for finding the length of the LIS algorithm in O(nlogn) complexity. public static int findLISLen(int[] a) { - int size = a.length; + final int size = a.length; + if (size == 0) { + return 0; + } int[] arr = new int[size]; arr[0] = a[0]; int lis = 1; for (int i = 1; i < size; i++) { - int index = binarySearchBetween(arr, lis, a[i]); + int index = binarySearchBetween(arr, lis - 1, a[i]); arr[index] = a[i]; - if (index > lis) { + if (index == lis) { lis++; } } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java new file mode 100644 index 000000000..ea7abed50 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java @@ -0,0 +1,49 @@ +package com.thealgorithms.dynamicprogramming; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; +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 LongestIncreasingSubsequenceTests { + @FunctionalInterface + public interface IntArrayToInt { + int apply(int[] array); + } + + @ParameterizedTest + @MethodSource("testCases") + public void testLongestIncreasingSubsequence(final int expected, final int[] input, final IntArrayToInt method) { + assertEquals(expected, method.apply(input)); + } + + private static Stream testCases() { + final Object[][] testData = { + {0, new int[] {}}, + {1, new int[] {1}}, + {1, new int[] {2, 2}}, + {1, new int[] {3, 3, 3}}, + {1, new int[] {4, 4, 4, 4}}, + {1, new int[] {5, 5, 5, 5, 5}}, + {2, new int[] {1, 2}}, + {2, new int[] {1, 2, 2, 2, 2}}, + {2, new int[] {1, 0, 2}}, + {3, new int[] {1, 10, 2, 30}}, + {3, new int[] {5, 8, 3, 7, 9, 1}}, + {6, new int[] {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}}, + {4, new int[] {10, 9, 2, 5, 3, 7, 101, 18}}, + {4, new int[] {10, 10, 9, 9, 2, 2, 5, 5, 3, 3, 7, 7, 101, 101, 18, 18}}, + {4, new int[] {0, 1, 0, 3, 2, 3}}, + {2, new int[] {1, 1, 2, 2, 2}}, + {3, new int[] {1, 1, 2, 2, 2, 3, 3, 3, 3}}, + }; + + final List methods = Arrays.asList(LongestIncreasingSubsequence::LIS, LongestIncreasingSubsequence::findLISLen); + + return Stream.of(testData).flatMap(input -> methods.stream().map(method -> Arguments.of(input[0], input[1], method))); + } +}