The Longest Increasing Subsequence (LIS) problem is one of the most famous algorithmic challenges. Given an unsorted array of integers, you must find the length of the longest strictly increasing subsequence. A subsequence allows you to delete elements from the array without changing the order of the remaining elements. For example, in [10, 9, 2, 5, 3, 7, 101, 18], the LIS is [2, 3, 7, 101], which has a length of 4.
LIS is the benchmark for testing a candidate's understanding of Dynamic Programming. Interviewers ask it because it has an intuitive DP solution that is great for junior roles, but also a hidden, highly elegant Binary Search solution that separates senior candidates from the pack. It is an essential building block; many other complex array problems can be reduced to finding an LIS.
There are two main patterns:
dp array where dp[i] is the length of the LIS ending at index i. Compare every element i with every element j before it.tails that stores the smallest tail of all increasing subsequences of various lengths. Iterate through the array; if an element is larger than the last element of tails, append it. If not, use Binary Search to find the smallest element in tails that is the current element, and replace it.Let's use the Binary Search approach on [10, 9, 2, 5, 3, 7].
We maintain a tails array.
tails is empty. Append 10. tails = [10].tails = [9]. (A subsequence of length 1 ending in 9 is better than one ending in 10).tails = [2].tails = [2, 5]. (Length is now 2).tails = [2, 3]. (We essentially swapped the tail of our length-2 sequence to be smaller, making it easier to append to later).tails = [2, 3, 7].
The length of the tails array is 3.A frequent misunderstanding with the approach is assuming that the resulting tails array represents the actual longest subsequence. It does not! It only accurately tracks the length of the longest subsequence and the optimal ending values. If the interviewer asks you to print the actual subsequence, you must keep track of parent pointers or rely on the DP array.
For the Longest Increasing Subsequence coding problem, practice both solutions. Start by writing the nested loop solution so you deeply understand the subproblems. Then, memorize the tails array Binary Search optimization. Being able to explain why replacing the elements in tails works (it lowers the barrier for future elements to join the sequence) will severely impress your interviewer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Shortest Distance to Target Color | Medium | Solve | |
| Sorting Three Groups | Medium | Solve | |
| Longest Arithmetic Subsequence | Medium | Solve | |
| House Robber IV | Medium | Solve | |
| Russian Doll Envelopes | Hard | Solve |