forked from PengFTang/Algorithms-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.java
More file actions
62 lines (53 loc) · 1.45 KB
/
LIS.java
File metadata and controls
62 lines (53 loc) · 1.45 KB
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
import java.util.ArrayList;
import java.util.Collections;
public class LIS {
private int[] maxLengthEndHere;
private int[] arr;
public LIS(int[] arr) {
this.arr = arr;
maxLengthEndHere = new int[arr.length];
}
private int getLISbottomup() {
return getLISbottomup(arr);
}
private int getLISbottomup(int[] arr) {
int max = 1;
maxLengthEndHere[0] = 1;
for(int i=1; i<arr.length; i++) {
int maxLength = 1;
for(int j=i-1; j>=0; j--) {
if(arr[i] >= arr[j] && maxLengthEndHere[j]+1 > maxLength) {
maxLength = maxLengthEndHere[j] + 1;
}
}
maxLengthEndHere[i] = maxLength;
max = maxLength > max ? maxLength : max;
}
return max;
}
private ArrayList<Integer> getLISbottomup(int max) {
ArrayList<Integer> list = new ArrayList<>();
int j=0;
for(int i=maxLengthEndHere.length-1; i>=0; i--) {
if(maxLengthEndHere[i] == max-j) {
list.add(arr[i]);
j++;
}
}
Collections.reverse(list);
return list;
}
public static void main(String[] args) {
int[] arr = {10, 0, 10, 30, 40, 45, 50, 10, 10, 10, 100, 100, 10, 11, 12, 101, 13, 105, 14, 16, 200};
int L = arr.length;
LIS lis = new LIS(arr);
int max = lis.getLISbottomup();
ArrayList<Integer> list = lis.getLISbottomup(max);
System.out.println("maximum length: " + max);
for(int i=0; i<L; i++)
System.out.print(lis.maxLengthEndHere[i] + "\t");
System.out.println();
System.out.println(list);
//System.out.println(lis.getSequence());
}
}