Longest Increasing Sub sequence
This is a classical problem that I came across a couple of hours ago, it intrigued me as I always knew the solution to be a DP approach of an O(n^2) .
When you shift your prospective into asking a different question you would find that it can be solved in O(N Log N) time, which is amazing in itself.
Introduction:-
I'll skip right into my observation, as there are a lot of resources covering different ways to tackle this problem, and I'll talk about my observation and hopefully it will shift your perspective too.
Summary:-
Increasing sub sequence has a property that it's a sorted array within an unordered array. this gives us the option to maintain an array with the sorted property with O(log N) cost in inserting non-ordered elements
Problem background:-
Given an array find the a sub sequence that is in increasing order
ex:-
input = [5,9,4,10,2,4]
output = 3 Explanation:- length of sub sequence [5,9,10] = 3 which is longer than [2,4], [4,10], [9,10]
Discussing different intuitions and approaches:-
Discussing implementation:-
When thinking about sorting problem usually what it comes to mind, do I have to sort the array first or not, however, sorting the array forces you to lose the property that you're looking for.
So what if you maintain a sorted version of the original array that you can always change according to the requirements of the problem.
looping through the original array
Justification:-
when you swap you risk getting a wrong representation of the longest increasing sub sequence, however it's justifiable as it fulfill the requirement
Example:-
with an array of [11, 22, 9] would result in a sorted array of [9, 22] of length 2 which still maintains the length property
Pseudo code
function lengthOfLIS_bsearch(originalArray):
n = length of originalArray
sortedReq = [originalArray[0]]
for i = 1 to n - 1:
if originalArray[i] > sortedReq[last element of sortedReq]:
sortedReq.push(originalArray[i])
else:
//looking for lower bound
low = binary search index of sortedReq where originalArray[i] can be inserted
sortedReq[low] = originalArray[i]
end
end for
return length of sortedReq
end function
Problems with this implementation:-
Resources