From 758def814afbbfcf6026495cfb8e80c0465a7ee0 Mon Sep 17 00:00:00 2001 From: jaynilshah <36128751+jaynilshah@users.noreply.github.com> Date: Thu, 31 Oct 2019 01:30:12 +0530 Subject: [PATCH] Longest Increasing Subsequence Longest Increasing Subsequence in O(nlogn) --- .../LongestIncreasingSubsequence.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Dynamic Programming/LongestIncreasingSubsequence.java diff --git a/Dynamic Programming/LongestIncreasingSubsequence.java b/Dynamic Programming/LongestIncreasingSubsequence.java new file mode 100644 index 0000000..e1821e7 --- /dev/null +++ b/Dynamic Programming/LongestIncreasingSubsequence.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; +import java.util.List; + +public class LIS { + public static int lowerBound(List 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 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 a){ + //equals included then upperbound + // else lower bound use as required + ArrayList ans = new ArrayList<>(); + for(int i=0;ians.size()){ + ans.add(a.get(i)); + } + else + ans.set(x,a.get(i)); + } + return ans.size(); + } + +} \ No newline at end of file