On this page
A subsequence keeps the order of a list but may skip terms. The longest increasing subsequence (LIS) is the longest one whose terms rise from left to right. Below, we find it for the first ten digits of π with both standard methods.
What is the longest increasing subsequence?
In 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, the subsequence 1, 4, 5, 6 rises, and no rising subsequence has five terms, so the LIS has length 4. The length is unique but the subsequence is not: 3, 4, 5, 9 also works. Here "increasing" means strictly, so the two 1s cannot both be used.
How does the O(n²) dynamic programme work?
Let L(i) be the length of the longest increasing subsequence ending at term i. Then L(i) = 1 + the largest L(j) over earlier terms smaller than term i, or 1 if there are none.
Left to right, the values of L are 1, 1, 2, 1, 3, 4, 2, 4, 3, 3. For example, 9 can follow 5 (L = 3), so its L is 4; 6 can also follow that 5, so it gets 4 too.
The answer is the largest L, which is 4. Store which j gave each maximum and walk back: 6 from 5, 5 from 4, 4 from 1, giving 1, 4, 5, 6. Each term checks every earlier term, about n²/2 comparisons: 45 here, but about 500 billion for a million terms.
How does patience sorting find it in O(n log n)?
Patience sorting is also a one-person card game; Aldous and Diaconis (1999) show how its analysis links the LIS of a random permutation to Young tableaux. Deal the numbers in order, placing each on the leftmost pile whose top is greater than or equal to it, or on a new pile at the right. The number of piles is the length of a longest increasing subsequence.
Our deal, with the pile tops after each card:
3 → [3]
1 → [1]
4 → [1, 4]
1 → [1, 4]
5 → [1, 4, 5]
9 → [1, 4, 5, 9]
2 → [1, 2, 5, 9]
6 → [1, 2, 5, 6]
5 → [1, 2, 5, 6]
3 → [1, 2, 3, 6]
Four piles: length 4 again. A pile's top never increases, so a rising subsequence takes at most one card per pile; and linking each card to the top of the pile on its left when it was placed traces a rising chain through every pile.
Pile tops always increase left to right, so each card needs only a binary search: O(n log n) overall, the bound the Wikipedia article on the problem gives.
What does the Erdős–Szekeres theorem say?
Erdős and Szekeres (1935) proved what is now stated as: any sequence of at least (r − 1)(s − 1) + 1 distinct real numbers has an increasing subsequence of length r or a decreasing one of length s. With r = s = 4, any 10 distinct numbers contain a monotone run of four.
Our digits repeat, so the theorem does not strictly apply, yet 1, 4, 5, 6 rises and 9, 6, 5, 3 falls. The proof labels each term with (longest rise ending here, longest fall ending here): distinct terms get distinct labels, and only nine labels have both entries at most 3. More in our explainer on Erdős–Szekeres and monotone subsequences.
How long is the LIS of a random permutation?
For a random permutation of n numbers, the expected LIS length is approximately 2√n, a result the Wikipedia article credits to Vershik and Kerov (1977) and that Samuels and Steele (1981) take as their starting point.
Samuels and Steele then studied a harder game: you must keep or skip each number as it arrives, without seeing the rest. They proved the best strategy gives an expected length of only about √(2n), and that no strategy does better. For n = 1,000,000 that is about 1,414 against 2,000: hindsight is worth a factor of about √2.
Baik, Deift and Johansson (1999) proved that the LIS length, suitably centred and scaled, converges to the Tracy–Widom distribution of the largest eigenvalue of a random GUE matrix. The link runs through Young tableaux, introduced in our post on the hook length formula.
How should you practise it?
Run both methods by hand on eight random digits; the lengths must match.
Code patience sorting with Python's bisect_left on the pile tops and test it on the digits above.
Allow equal terms and see why bisect_right is then correct.
If the recurrence or the binary search is where you stall, the Fermi Concept Library gives every concept in mathematics and computing its own lesson, and the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer. For more on order inside permutations, see our piece on permutation pattern avoidance.



