代码拉取完成,页面将自动刷新
/**
* 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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。