forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestIncreasingSubsequence.java
More file actions
27 lines (26 loc) · 968 Bytes
/
Copy pathLongestIncreasingSubsequence.java
File metadata and controls
27 lines (26 loc) · 968 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
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
//int[] arr = {50, 3, 10, 7, 40, 80};
int[] arr = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
int result = getLongestSub(arr);
System.out.println(result);
}
private static int getLongestSub(int[] arr) {
if(arr==null || arr.length==0)
return 0;
int[] lengthStore = new int[arr.length];
for (int i = 0; i < arr.length; i++)
lengthStore[i] = 1;
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < i; j++)
if (arr[i] > arr[j] && lengthStore[i] < lengthStore[j] + 1)
lengthStore[i] = lengthStore[j] + 1;
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < lengthStore.length; i++) {
if (max < lengthStore[i])
max = lengthStore[i];
}
return max;
}
}