Sub topic 1
What Is a Longest Increasing Subsequence?
The longest increasing subsequence (LIS) problem is a classic of algorithm design. It is simple to state and has a short, fast solution. It also shows up inside many problems that do not look like it at first. This chapter fixes the vocabulary used for the rest of the tutorial. It covers what a subsequence is, what "increasing" means in its strict and non-strict forms, and which answers count as correct. Later chapters build algorithms on these definitions, so it pays to get them exact now.
Prerequisites
This chapter assumes you can:
- Read and write basic Python, including functions, loops, and list slicing.
- Work with arrays (Python lists) and zero-based indexing.
- Reason about Big-O time and space complexity.
- Explain binary search on a sorted array. This chapter does not use it, but later chapters depend on it.
- Recognize basic dynamic programming: breaking a problem into subproblems linked by a recurrence.
You do not need any prior exposure to LIS itself.
Subsequences, Substrings, and Subsets
Take a sequence a of length n. A subsequence of a is what remains after deleting zero or more elements while keeping the rest in their original order. Formally, choose indices
i₁ < i₂ < … < iₖ
and read off a[i₁], a[i₂], …, a[iₖ]. The indices must increase. The chosen positions do not need to be next to each other.
Two related ideas are often confused with subsequences:
| Concept | Order preserved? | Must be contiguous? | Number of choices for length n |
|---|---|---|---|
| Substring (subarray) | Yes | Yes | n(n+1)/2 non-empty, plus the empty one |
| Subsequence | Yes | No | 2ⁿ (counting the empty one) |
| Subset | No | No | 2ⁿ (as sets of positions) |
The difference matters in practice. In [3, 1, 4, 1, 5], the list [3, 4, 5] is a subsequence (indices 0, 2, 4) but not a subarray. The list [1, 3] is not a subsequence: the only 3 is at index 0, and both 1s come after it. A subset ignores order, so {1, 3} is a fine subset, but subsets are the wrong model for this problem.
A subsequence is defined by the indices you pick. When values repeat, two different index choices can produce the same list of values. In [1, 2, 1, 2], the value list [1, 2] comes from indices (0, 1), (0, 3), or (2, 3). This matters once we start counting subsequences in a later chapter.
What "Increasing" Means
A sequence s is strictly increasing if every element is smaller than the one after it:
s[0] < s[1] < … < s[k−1]
It is non-decreasing (sometimes called "weakly increasing") if equal neighbours are allowed:
s[0] ≤ s[1] ≤ … ≤ s[k−1]
Unless this tutorial says otherwise, "increasing" means strictly increasing. This matches the usual convention in textbooks and on competitive programming judges. When a problem says "increasing," check which version it means. On inputs with repeated values the two versions give different answers, and implementations that differ by one comparison operator will disagree.
By convention, the empty sequence and every single-element sequence count as increasing under both definitions. There are no adjacent pairs, so no comparison can fail.
The LIS Problem
Given a sequence a, the longest increasing subsequence problem asks for an increasing subsequence of a whose length is as large as possible. The problem has two common forms:
- Length version: return the maximum length
L. - Witness version: return one actual subsequence of length
L.
The length is unique. The subsequence that achieves it often is not. A correct witness algorithm may return any one of the optimal subsequences, so a test that checks for one specific list is usually a wrong test.
A Worked Example
The running example for this tutorial is:
a = [10, 9, 2, 5, 3, 7, 101, 18]The LIS length is 4. All of the following are valid answers to the witness version:
| Subsequence | Indices |
|---|---|
| [2, 5, 7, 101] | 2, 3, 5, 6 |
| [2, 5, 7, 18] | 2, 3, 5, 7 |
| [2, 3, 7, 101] | 2, 4, 5, 6 |
| [2, 3, 7, 18] | 2, 4, 5, 7 |
No increasing subsequence of length 5 exists. Here is a short argument. Neither 10 nor 9 can come before 2, 5, 3, or 7, because they are larger. So any chain that starts with 10 or 9 can only be followed by 101 or 18, which gives at most length 2. Without 10 and 9, you have [2, 5, 3, 7, 101, 18]. Among these, 5 and 3 cannot both be used, because 3 comes after 5 and is smaller. The values 101 and 18 cannot both be used either. That leaves at most 2, one of {5, 3}, 7, and one of {101, 18}: four elements.
Checking a Candidate Answer in Code
Before writing an algorithm that finds an LIS, write a checker that verifies one. This turns the definitions into code. You can also use it later to test faster algorithms against random inputs. The code below checks order with a two-pointer scan and checks increase with a comparison over adjacent pairs.
def is_subsequence(b, a):
"""Return True if b can be obtained from a by deleting elements."""
j = 0 # next position in a to search from
for x in b:
while j < len(a) and a[j] != x:
j += 1
if j == len(a):
return False
j += 1 # consume a[j]; the next element of b must come later
return True
def is_increasing(s, strict=True):
"""Return True if s is strictly increasing (or non-decreasing if strict=False)."""
if strict:
return all(x < y for x, y in zip(s, s[1:]))
return all(x <= y for x, y in zip(s, s[1:]))
def is_increasing_subsequence(b, a, strict=True):
return is_subsequence(b, a) and is_increasing(b, strict)
a = [10, 9, 2, 5, 3, 7, 101, 18]
print(is_increasing_subsequence([2, 5, 7, 101], a)) # True
print(is_increasing_subsequence([2, 3, 7, 18], a)) # True
print(is_increasing_subsequence([2, 7, 5], a)) # False: not increasing
print(is_increasing_subsequence([5, 2, 7], a)) # False: wrong order in a
print(is_increasing_subsequence([], a)) # True: empty is valid
b = [1, 3, 3, 2, 3]
print(is_increasing_subsequence([1, 3, 3, 3], b)) # False (strict)
print(is_increasing_subsequence([1, 3, 3, 3], b, strict=False)) # TrueThe greedy scan in is_subsequence is correct because matching each element of b at the earliest possible position in a never hurts. It leaves the most room for the elements that follow. The check runs in O(len(a)) time. The increasing check runs in O(len(b)).
Edge Cases You Should Know Cold
Many bugs in LIS code come from inputs at the boundaries. Know the expected answers before you write any algorithm.
| Input | Strict LIS length | Non-decreasing length | Reason |
|---|---|---|---|
| `[]` | 0 | 0 | Only the empty subsequence exists |
| `[42]` | 1 | 1 | A single element is increasing |
| `[1, 2, 3, 4, 5]` | 5 | 5 | Already sorted: the whole array |
| `[5, 4, 3, 2, 1]` | 1 | 1 | Every pair is out of order |
| `[7, 7, 7, 7]` | 1 | 4 | Equal values break strictness only |
| `[1, 3, 3, 2, 3]` | 3 | 4 | e.g. [1, 2, 3] vs [1, 3, 3, 3] |
Some properties follow directly from the definitions:
- For any non-empty input, 1 ≤ L ≤ n. The lower bound holds because any one element is a valid answer.
- L = n exactly when the array is sorted under the chosen comparison.
- The strict LIS is never longer than the non-decreasing one, because every strictly increasing sequence is also non-decreasing.
- LIS depends only on the relative order of the values. Replacing every value with its rank (with equal values sharing a rank) leaves the answer unchanged. A later chapter uses this idea, called coordinate compression.
Related Variants
A few neighbouring problems reduce to LIS with small changes:
- Longest decreasing subsequence: negate every value, or reverse the array, then run LIS.
- Longest non-decreasing subsequence: change the comparison. In the binary search version, this means choosing a different bisect bound. A later chapter covers this.
- LIS of pairs or objects: define "less than" on the objects, often after sorting by one coordinate. Nesting envelopes is the standard example.
The way forward follows directly from the definition. A brute-force search would try all 2ⁿ subsequences. The next chapter shows why that is hopeless. After that, a dynamic program uses the ordered-indices definition to reach O(n²), and a sharper invariant brings this down to O(n log n).
Exercise
For a = [3, 1, 4, 1, 5, 9, 2, 6], give the strict LIS length and one witness, and say whether the witness is unique.
Show answer
The length is 4. One witness is [3, 4, 5, 9] (indices 0, 2, 4, 5). It is not unique: [1, 4, 5, 9], [1, 4, 5, 6], [3, 4, 5, 6], and [1, 2, 6] extended... more carefully, [1, 4, 5, 9] and [3, 4, 5, 6] are also valid length-4 answers. No length-5 answer exists, because after 5 only one of 9 and 6 can be used (6 comes after 9 and is smaller), and 2 cannot sit between 4 and 5.
Exercise
Is [1, 1] a subsequence of [1, 2, 1]? Is it a strictly increasing subsequence? A non-decreasing one?
Show answer
Yes, it is a subsequence (indices 0 and 2). It is not strictly increasing, because 1 < 1 is false. It is non-decreasing, because 1 ≤ 1, so it is a valid answer for the non-decreasing variant. The non-decreasing LIS of [1, 2, 1] has length 2, from either [1, 2] or [1, 1].
Exercise
Without running code, give the strict and non-decreasing LIS lengths of [2, 2, 1, 1, 3, 3].
Show answer
Strict: 2, for example [2, 3] or [1, 3]. Equal values cannot both be used, and only one value from each of the levels {1, 2} can come before a 3. Non-decreasing: 4, from [2, 2, 3, 3] or [1, 1, 3, 3]. The 2s and the 1s cannot be mixed, because every 1 comes after every 2.