Skip to content

Commit f6a580a

Browse files
authored
Merge pull request darpanjbora#97 from jaynilshah/master
Longest Increasing Subsequence
2 parents c57edb6 + 758def8 commit f6a580a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class LIS {
5+
public static int lowerBound(List<Integer> a, int low, int high, int element){
6+
while(low < high){
7+
int middle = low + (high - low)/2;
8+
if(element > a.get(middle))
9+
low = middle + 1;
10+
else
11+
high = middle;
12+
}
13+
return low;
14+
}
15+
16+
public static int upperBound(List<Integer> a, int low, int high, int element){
17+
while(low < high){
18+
int middle = low + (high - low)/2;
19+
if(a.get(middle) > element)
20+
high = middle;
21+
else
22+
low = middle + 1;
23+
}
24+
return low;
25+
}
26+
27+
public static int longestIncreasingSubsequence(List<Integer> a){
28+
//equals included then upperbound
29+
// else lower bound use as required
30+
ArrayList<Integer> ans = new ArrayList<>();
31+
for(int i=0;i<a.size();i++){
32+
int x = lowerBound(ans,0,ans.size(),a.get(i));
33+
if(x+1>ans.size()){
34+
ans.add(a.get(i));
35+
}
36+
else
37+
ans.set(x,a.get(i));
38+
}
39+
return ans.size();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)