forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestIncreasingSubsequence.java
45 lines (37 loc) · 1.41 KB
/
LongestIncreasingSubsequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 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, [email protected]
*/
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;
}
}