Longest Increasing Subsequence
LIS
Last updated
LIS
Last updated
LIS Problem: Find the length of the longest increasing subsequence in an array of elements N
Dynamic Programming is suitable for this problem because we can compute the LIS that ends at each element in the array. All we need to compute the next one is to increase the LIS in any previous elements by 1.
Notes: If we want to restore and find the actual subsequence itself rather than the length, we can just have a separate array storing the index of the second to last LIS we use to compute the current one. Sort of like an ancestor array.
Thus, for every new element arr[i]
, we need to use it to update the dp array as if we are inserting the new element to a sequence. For every new element, we try to insert it at the longest sequence where the last element is smaller than arr[i]
.
We define the array and as the length of the longest subsequence that ends with the element .
Thus: for j < i
We loop through all dp[j] before i to find the maximum value, which would take
We define a different array where stores the smallest last element of a LIS with length i