forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.java
More file actions
41 lines (38 loc) · 1008 Bytes
/
LIS.java
File metadata and controls
41 lines (38 loc) · 1008 Bytes
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
import java.util.*;
import java.io.*;
class LIS {
public static void printSolution(int[] dp, int[] track, int[] seq, int start) {
Stack<Integer> stck = new Stack<Integer>();
while(true) {
stck.push(seq[start]);
if(dp[start] == 1) break;
start = track[start];
}
while(stck.isEmpty() == false) {
System.out.print(stck.pop() + " ");
}
System.out.println();
}
public static void LongestIncreasingSubsequence(int[] seq) {
int[] dp = new int[seq.length];
int[] track = new int[seq.length];
Arrays.fill(dp, 1);
int max_ind = 0;
for(int i=0; i<dp.length; i++) {
for(int j=i+1; j<dp.length; j++) {
if(seq[j] > seq[i] && dp[j] < dp[i]+1) {
dp[j] = dp[i] + 1;
track[j] = i;
}
}
if(dp[i] > dp[max_ind])
max_ind = i;
}
System.out.print("Length of LIS = " + dp[max_ind] + "\nSequence = ");
printSolution(dp, track, seq, max_ind);
}
public static void main(String args[]) {
int[] seq = {3, 2, 4, 6};
LongestIncreasingSubsequence(seq);
}
}