Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longest Increasing Subsequence #97

Merged
merged 1 commit into from
Nov 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Dynamic Programming/LongestIncreasingSubsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}

}