Ai
1 Star 0 Fork 3

aiobc/Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LongestIncreasingSubsequence.java 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
Rafael Chaves 提交于 2020-02-03 02:07 +08:00 . Adopt standard Java project layout (#120)
/**
* This file shows you how to find the length of the longest increasing subsequence length, using
* dynamic programming. Time complexity: O(n^2)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.dp;
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
System.out.println(lis(new int[] {1, 3, 2, 4, 3})); // 3
System.out.println(lis(new int[] {2, 7, 4, 3, 8})); // 3
System.out.println(lis(new int[] {5, 4, 3, 2, 1})); // 1
System.out.println(lis(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9})); // 9
}
// Finds the length of the longest increasing subsequence length, O(n^2)
public static int lis(int[] ar) {
if (ar == null || ar.length == 0) return 0;
int n = ar.length, len = 0;
// When starting, each individual element has a LIS
// of exactly one, so each index is initialized to 1
int[] dp = new int[n];
java.util.Arrays.fill(dp, 1);
// Processing the array left to right update the value of dp[j] if two
// conditions hold 1) The value at i is less than that of the one at j
// and 2) updating the value of dp[j] to dp[i]+1 is better
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (ar[i] < ar[j] && dp[j] < dp[i] + 1) {
dp[j] = dp[i] + 1;
}
}
// Track the LIS
if (dp[i] > len) len = dp[i];
}
return len;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiobc/Algorithms.git
git@gitee.com:aiobc/Algorithms.git
aiobc
Algorithms
Algorithms
master

搜索帮助