1 Star 0 Fork 0

蔡茂昌 / Algorithms-implemented-in-Java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LongestIncreasingSubsequence.java 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Daniel Müller 提交于 2017-10-03 18:26 . turned some public methods private
import java.util.Scanner;
/**
*
* @author Afrizal Fikri (https://github.com/icalF)
*
*/
public class LongestIncreasingSubsequence {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = sc.nextInt();
}
System.out.println(LIS(ar));
}
private static int upperBound(int[] ar, int l, int r, int key) {
while (l < r-1) {
int m = (l + r) / 2;
if (ar[m] >= key)
r = m;
else
l = m;
}
return r;
}
private static int LIS(int[] array) {
int N = array.length;
if (N == 0)
return 0;
int[] tail = new int[N];
int length = 1; // always points empty slot in tail
tail[0] = array[0];
for (int i = 1; i < N; i++) {
// new smallest value
if (array[i] < tail[0])
tail[0] = array[i];
// array[i] extends largest subsequence
else if (array[i] > tail[length-1])
tail[length++] = array[i];
// array[i] will become end candidate of an existing subsequence or
// Throw away larger elements in all LIS, to make room for upcoming grater elements than array[i]
// (and also, array[i] would have already appeared in one of LIS, identify the location and replace it)
else
tail[upperBound(tail, -1, length-1, array[i])] = array[i];
}
return length;
}
}
Java
1
https://gitee.com/edmondcmc/Algorithms-implemented-in-Java.git
git@gitee.com:edmondcmc/Algorithms-implemented-in-Java.git
edmondcmc
Algorithms-implemented-in-Java
Algorithms-implemented-in-Java
master

搜索帮助