-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathLIS.java
42 lines (38 loc) · 1.15 KB
/
LIS.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
import java.util.ArrayList;
import java.util.List;
public class LIS {
public static int lowerBound(List<Integer> a, int low, int high, int element){
while(low < high){
int middle = low + (high - low)/2;
if(element > a.get(middle))
low = middle + 1;
else
high = middle;
}
return low;
}
public static int upperBound(List<Integer> a, int low, int high, int element){
while(low < high){
int middle = low + (high - low)/2;
if(a.get(middle) > element)
high = middle;
else
low = middle + 1;
}
return low;
}
public static int longestIncreasingSubsequence(List<Integer> a){
//equals included then upperbound
// else lower bound use as required
ArrayList<Integer> ans = new ArrayList<>();
for(int i=0;i<a.size();i++){
int x = lowerBound(ans,0,ans.size(),a.get(i));
if(x+1>ans.size()){
ans.add(a.get(i));
}
else
ans.set(x,a.get(i));
}
return ans.size();
}
}