-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestIncreasingSubsequence.java
79 lines (72 loc) · 2.37 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Important dp question
import java.util.*;
class LongestIncreasingSubsequence {
public int longestSubsequenceWithActualSolution(int arr[]){
int T[] = new int[arr.length];
int actualSolution[] = new int[arr.length];
for(int i=0; i < arr.length; i++){
T[i] = 1;
actualSolution[i] = i;
}
for(int i=1; i < arr.length; i++){
for(int j=0; j < i; j++){
if(arr[i] > arr[j]){
if(T[j] + 1 > T[i]){
T[i] = T[j] + 1;
//set the actualSolution to point to guy before me
actualSolution[i] = j;
}
}
}
}
//find the index of max number in T
int maxIndex = 0;
for(int i=0; i < T.length; i++){
if(T[i] > T[maxIndex]){
maxIndex = i;
}
}
//lets print the actual solution
int t = maxIndex;
int newT = maxIndex;
do{
t = newT;
System.out.print(arr[t] + " ");
newT = actualSolution[t];
}while(t != newT);
System.out.println();
return T[maxIndex];
}
/**
* Recursive way of solving LIS
*/
public int longestSubsequenceRecursive(int arr[]){
int maxLen = 0;
for(int i=0; i < arr.length-1; i++){
int len = longestSubsequenceRecursive(arr,i+1,arr[i]);
if(len > maxLen){
maxLen = len;
}
}
return maxLen + 1;
}
private int longestSubsequenceRecursive(int arr[], int pos, int lastNum){
if(pos == arr.length){
return 0;
}
int t1 = 0;
if(arr[pos] > lastNum){
t1 = 1 + longestSubsequenceRecursive(arr, pos+1, arr[pos]);
}
int t2 = longestSubsequenceRecursive(arr, pos+1, lastNum);
return Math.max(t1, t2);
}
public static void main(String args[]){
LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
int arr[] = {23,10,22,5,33,8,9,21,50,41,60,80,99, 22,23,24,25,26,27};
int result = lis.longestSubsequenceWithActualSolution(arr);
int result1 = lis.longestSubsequenceRecursive(arr);
System.out.println(result);
System.out.println(result1);
}
}