mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@ -17,7 +17,6 @@ public class LongestIncreasingSubsequence {
|
|||||||
System.out.println(LIS(arr));
|
System.out.println(LIS(arr));
|
||||||
System.out.println(findLISLen(arr));
|
System.out.println(findLISLen(arr));
|
||||||
sc.close();
|
sc.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int upperBound(int[] ar, int l, int r, int key) {
|
private static int upperBound(int[] ar, int l, int r, int key) {
|
||||||
@ -58,40 +57,33 @@ public class LongestIncreasingSubsequence {
|
|||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @author Alon Firestein (https://github.com/alonfirestein) */
|
/** @author Alon Firestein (https://github.com/alonfirestein) */
|
||||||
|
|
||||||
// A function for finding the length of the LIS algorithm in O(nlogn) complexity.
|
// A function for finding the length of the LIS algorithm in O(nlogn) complexity.
|
||||||
public static int findLISLen(int a[]) {
|
public static int findLISLen(int a[]) {
|
||||||
int size = a.length;
|
int size = a.length;
|
||||||
int arr[] = new int[size];
|
int arr[] = new int[size];
|
||||||
arr[0] = a[0];
|
arr[0] = a[0];
|
||||||
int lis = 1;
|
int lis = 1;
|
||||||
for (int i = 1; i < size; i++) {
|
for (int i = 1; i < size; i++) {
|
||||||
int index = binarySearchBetween(arr, lis, a[i]);
|
int index = binarySearchBetween(arr, lis, a[i]);
|
||||||
arr[index] = a[i];
|
arr[index] = a[i];
|
||||||
if (index > lis)
|
if (index > lis) lis++;
|
||||||
lis++;
|
}
|
||||||
}
|
return lis;
|
||||||
return lis;
|
}
|
||||||
}
|
|
||||||
// O(logn)
|
// O(logn)
|
||||||
private static int binarySearchBetween(int[] t, int end, int key) {
|
private static int binarySearchBetween(int[] t, int end, int key) {
|
||||||
int left = 0;
|
int left = 0;
|
||||||
int right = end;
|
int right = end;
|
||||||
if (key < t[0])
|
if (key < t[0]) return 0;
|
||||||
return 0;
|
if (key > t[end]) return end + 1;
|
||||||
if (key > t[end])
|
while (left < right - 1) {
|
||||||
return end + 1;
|
int middle = (left + right) / 2;
|
||||||
while (left < right - 1) {
|
if (t[middle] < key) left = middle;
|
||||||
int middle = (left + right) / 2;
|
else right = middle;
|
||||||
if (t[middle] < key)
|
}
|
||||||
left = middle;
|
return right;
|
||||||
else
|
}
|
||||||
right = middle;
|
|
||||||
}
|
|
||||||
return right;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user