Sub topic 1
What Is a Longest Increasing Subsequence?
The longest increasing subsequence (LIS) problem shows up in coding interviews, in diff tools, and inside many larger algorithms. This chapter defines the problem carefully, because most bugs in LIS code come from a fuzzy definition. Later chapters cover how to compute it.
Prerequisites
This chapter assumes you can:
- Read and write basic Python, including loops,
zip, and list slicing. - Work with arrays and zero-based indexing.
- Reason about Big-O time and space complexity.
The later chapters also use binary search and basic dynamic programming. You do not need either one here.
Subsequences
A subsequence of an array is what remains after you delete zero or more elements without reordering the rest. The kept elements do not have to be adjacent. They only have to appear in their original order.
Take a = [10, 9, 2, 5, 3, 7, 101, 18]:
| Candidate | Subsequence of `a`? | Reason |
|---|---|---|
| `[2, 5, 7, 101]` | Yes | Indices 2, 3, 5, 6 are in order |
| `[9, 3, 18]` | Yes | Gaps between elements are allowed |
| `[5, 2, 7]` | No | 5 comes after 2 in `a` |
| `[]` | Yes | Deleting everything is allowed |
Don't confuse a subsequence with a substring (also called a subarray), which must be contiguous. [2, 5, 3] is both a subarray and a subsequence. [2, 3, 7] is only a subsequence.
Increasing: strict or non-decreasing
A sequence is strictly increasing if every element is greater than the one before it: x[0] < x[1] < .... A sequence is non-decreasing if equal neighbours are also allowed: x[0] <= x[1] <= ....
Unless a problem says otherwise, "longest increasing subsequence" means strictly increasing. The difference matters whenever the input has duplicates. For [1, 3, 3, 2, 3], the longest strictly increasing subsequence is [1, 2, 3] (length 3). The longest non-decreasing one is [1, 3, 3, 3] (length 4).
The problem statement
Given an array a of length n, find the maximum length of a strictly increasing subsequence of a. Sometimes the task also asks for one such subsequence.
For [10, 9, 2, 5, 3, 7, 101, 18] the answer is 4. Several subsequences reach that length: [2, 5, 7, 101], [2, 3, 7, 18], [2, 5, 7, 18], and [2, 3, 7, 101]. The length is unique, but the subsequence usually isn't. So code that returns a subsequence can be correct even when it differs from the one in an example.
Checking a candidate in code
Testing whether a proposed answer is valid is easier than finding the best one. It's also useful for testing the faster algorithms in later chapters. The function below matches elements in a single left-to-right pass, so it runs in O(n) time.
def is_subsequence(sub, a):
i = 0 # next position in sub still waiting to be matched
for x in a:
if i < len(sub) and sub[i] == x:
i += 1
return i == len(sub)
def is_increasing(seq, strict=True):
if strict:
return all(x < y for x, y in zip(seq, seq[1:]))
return all(x <= y for x, y in zip(seq, seq[1:]))
a = [10, 9, 2, 5, 3, 7, 101, 18]
print(is_subsequence([2, 5, 7, 101], a)) # True
print(is_subsequence([5, 2, 7], a)) # False
print(is_increasing([2, 3, 7, 18])) # True
print(is_increasing([1, 3, 3, 3])) # False
print(is_increasing([1, 3, 3, 3], False)) # TrueA candidate is a valid answer if both checks pass and its length equals the maximum. Matching greedily is safe here. If sub fits into a at all, then matching each element at its earliest possible position also succeeds.
Common errors
- Requiring the chosen elements to be contiguous. That's a different, easier problem.
- Sorting the input first. Sorting destroys the order that defines a subsequence.
- Assuming the answer is unique and comparing your output with one fixed list in tests. Compare lengths, and check validity with functions like the ones above.
- Forgetting the edge cases. An empty array has LIS length 0, and any non-empty array has LIS length at least 1.
Exercise
For a = [3, 1, 2, 2, 4], what is the length of the longest strictly increasing subsequence, and what is the length of the longest non-decreasing subsequence? Give one example of each.
Show answer
Strictly increasing: length 3, for example [1, 2, 4]. Non-decreasing: length 4, namely [1, 2, 2, 4]. The two equal 2s can both be used only when equal neighbours are allowed.
Exercise
Is [9, 5, 101] a subsequence of [10, 9, 2, 5, 3, 7, 101, 18]? Is it an increasing subsequence?
Show answer
It is a subsequence, because 9, 5 and 101 appear at indices 1, 3 and 6, in that order. It is not increasing, because 9 > 5.