mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 22:22:21 +08:00
fix: handle constant inputs in `LongestIncreasingSubsequence::findLISLen' (#5160)
fix: handle constant inputs in `LongestIncreasingSubsequence::findLISLen`
This commit is contained in:
@ -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++;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user