Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b1265f

Browse files
Longest Increasing Subsequence
Longest Increasing Subsequence in O(nlogn) in Java
1 parent 9ef5eba commit 9b1265f

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)

AltStyle によって変換されたページ (->オリジナル) /