Skip to content

Commit 886383d

Browse files
committed
fix: handle constant inputs in LongestIncreasingSubsequence::findLISLen
1 parent bbef89c commit 886383d

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java

+7-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
import java.util.Scanner;
4-
53
/**
64
* @author Afrizal Fikri (https://github.com/icalF)
75
*/
86
public final class LongestIncreasingSubsequence {
97
private LongestIncreasingSubsequence() {
108
}
119

12-
public static void main(String[] args) {
13-
Scanner sc = new Scanner(System.in);
14-
int n = sc.nextInt();
15-
16-
int[] arr = new int[n];
17-
for (int i = 0; i < n; i++) {
18-
arr[i] = sc.nextInt();
19-
}
20-
21-
System.out.println(LIS(arr));
22-
System.out.println(findLISLen(arr));
23-
sc.close();
24-
}
25-
2610
private static int upperBound(int[] ar, int l, int r, int key) {
2711
while (l < r - 1) {
2812
int m = (l + r) >>> 1;
@@ -36,7 +20,7 @@ private static int upperBound(int[] ar, int l, int r, int key) {
3620
return r;
3721
}
3822

39-
private static int LIS(int[] array) {
23+
public static int LIS(int[] array) {
4024
int N = array.length;
4125
if (N == 0) {
4226
return 0;
@@ -73,14 +57,17 @@ else if (array[i] > tail[length - 1]) {
7357
*/
7458
// A function for finding the length of the LIS algorithm in O(nlogn) complexity.
7559
public static int findLISLen(int[] a) {
76-
int size = a.length;
60+
final int size = a.length;
61+
if (size == 0) {
62+
return 0;
63+
}
7764
int[] arr = new int[size];
7865
arr[0] = a[0];
7966
int lis = 1;
8067
for (int i = 1; i < size; i++) {
81-
int index = binarySearchBetween(arr, lis, a[i]);
68+
int index = binarySearchBetween(arr, lis - 1, a[i]);
8269
arr[index] = a[i];
83-
if (index > lis) {
70+
if (index == lis) {
8471
lis++;
8572
}
8673
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.Stream;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class LongestIncreasingSubsequenceTests {
13+
@FunctionalInterface
14+
public interface IntArrayToInt {
15+
int apply(int[] array);
16+
}
17+
18+
@ParameterizedTest
19+
@MethodSource("testCases")
20+
public void testLongestIncreasingSubsequence(final int expected, final int[] input, final IntArrayToInt method) {
21+
assertEquals(expected, method.apply(input));
22+
}
23+
24+
private static Stream<Arguments> testCases() {
25+
final Object[][] testData = {
26+
{0, new int[] {}},
27+
{1, new int[] {1}},
28+
{1, new int[] {2, 2}},
29+
{1, new int[] {3, 3, 3}},
30+
{1, new int[] {4, 4, 4, 4}},
31+
{1, new int[] {5, 5, 5, 5, 5}},
32+
{2, new int[] {1, 2}},
33+
{2, new int[] {1, 2, 2, 2, 2}},
34+
{2, new int[] {1, 0, 2}},
35+
{3, new int[] {1, 10, 2, 30}},
36+
{3, new int[] {5, 8, 3, 7, 9, 1}},
37+
{6, new int[] {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}},
38+
{4, new int[] {10, 9, 2, 5, 3, 7, 101, 18}},
39+
{4, new int[] {10, 10, 9, 9, 2, 2, 5, 5, 3, 3, 7, 7, 101, 101, 18, 18}},
40+
{4, new int[] {0, 1, 0, 3, 2, 3}},
41+
{2, new int[] {1, 1, 2, 2, 2}},
42+
{3, new int[] {1, 1, 2, 2, 2, 3, 3, 3, 3}},
43+
};
44+
45+
final List<IntArrayToInt> methods = Arrays.asList(LongestIncreasingSubsequence::LIS, LongestIncreasingSubsequence::findLISLen);
46+
47+
return Stream.of(testData).flatMap(input -> methods.stream().map(method -> Arguments.of(input[0], input[1], method)));
48+
}
49+
}

0 commit comments

Comments
 (0)