Problem - Given an array of integers. Find the longest increasing subsequence in the array. Eg. [42, 91, 46, 73, 75, 91, 95]. The LIS for this array is 6 which is [42,46,73,75,91,95]. Note: Subsequence does not require the elements to be contiguous
As always, we begin with our naive solution which is a DFS/Bruteforce search trying every possible combination until we get the longest sequence. This is simply exhaustive and will explode on bigger arrays. Time Complexity : O(2^n) ~ Exponential
Now, If you notice we repeatedly try a prefix subsequence repeatedly. This tells us, the problem is a good candidate for a dynamic programming algorithm.
We define lis[0...L], where L is the length of the array. We define lis[i] as the length of longest increasing subsequence that ends at i.
- lis[0] = 1 { trivial since, the longest increasing sequence that ends at the start of the array contains just the first element, also true for the arr[i] where arr[i] is the smaller than preceding elements}
- lis[i] = 1 + max( lis[j = 0..i-1] where A[i] > A[j] )
To build back the LIS, we also store a prev[] array, which holds the preceding index of the previous member of the LIS. The prev for the first element will be marked as negative to indicate termination. We build the LIS backwards and finally reverse the sequence.
Time Complexity : O(n^2)
I'll end this post here, but I will follow up with a O(nlogn) method which can be used to solve the problem as well. I will also describe the applications and some problems you can solve using LIS. Until next time.
Comments
Post a Comment