Definition · Longest increasing subsequenceA subsequence of a sequence is what is left after deleting zero or more entries, keeping the rest in order. It is strictly increasing if each entry is larger than the one before. The problem is to find a longest strictly increasing subsequence; there may be many, and the lesson is careful never to call one of them the answer.
Theorem · The per-position recurrenceLet best(i) be the length of the longest strictly increasing subsequence ending at position i. Then best(i) = 1 if no earlier position holds a smaller value, and otherwise best(i) = 1 + max of best(j) over positions j < i with a(j) < a(i). The answer is the maximum over all i.
A longest increasing subsequence ending at i has some second-to-last entry, at a position j < i with a(j) < a(i), unless it consists of a(i) alone. Removing a(i) leaves an increasing subsequence ending at j, and it must be a longest one, or substituting a longer one and re-appending a(i) would beat the original.
So the length is 1 + best(j) for that j, and taking the maximum over all admissible j gives best(i). Every increasing subsequence ends somewhere, so the overall answer is the largest best(i), and the predecessor recorded at each step reconstructs one witness for it.
□
What the other algorithm maintains
The binary-search method processes the values in order and keeps an array tails in which position L holds the smallest value that ends an increasing subsequence of length L + 1 among the values processed so far. A new value either extends the array, when it is larger than everything in it, or replaces the first entry that is at least as large. Both cases are found by binary search, so the whole run is Θ(n log n) comparisons — 14 on ten values, against the table's 45.
Nothing in that description promises the array is a subsequence of the input. It is a record of what is achievable at each length, and its entries may come from positions in any order, including positions that cannot coexist in one run. The length is right, because the array's length is exactly the longest achievable, and the contents are a different question.
Example · Ten values, and the array that changed its mindOn 10 9 2 5 3 7 101 18 4 8 the tails array after the seventh value is 2 3 7 101, which is a genuine longest increasing subsequence. Then 18 replaces 101, then 4 replaces 7, then 8 replaces 18, and the array ends as 2 3 4 8. The table's reconstruction is 2 5 7 101. Both have length 4 and both are valid answers here; the array passed through one valid answer on its way to a different one, and nothing about the mechanism guarantees the destination is valid.
That last sentence is the whole lesson, and the lab has the instance that proves it. Switch the sequence to 3 1 4 1 5 9 2 6 5 3. The tails array ends 1 2 3 6, which is increasing; the panel's second check asks whether it is a subsequence of the input, and the answer is no — 1, 2 and 3 occur in that order, and the only 6 sits before the last 3. The table's reconstruction is 3 4 5 9, which passes both checks.
Why two checks, and why the weaker one never fires
The panel reports is increasing and is a subsequence of the input as separate verdicts. The first is satisfied by the tails array on every input, because the invariant forces it, and by a sorted copy of the input too — which is why it cannot be the only check. The second is the one with teeth. A reconstruction that returns the right values in the wrong positions fails it, and so does an array that was never a run in the first place.
The third route is the enumeration: every one of the 2ⁿ subsequences, tested for increase, with the longest kept. On ten values that is 1 024 subsets, which is instant, and the lab refuses above sixteen values. It agrees with both algorithms on the length, which is what makes the comparison of the two arrays a comparison rather than a guess.
The measured comparison counts are 45 and 14 on ten values. The proved bounds are Θ(n²) and Θ(n log n), and the enumeration is Θ(2ⁿ n). Here the three measured numbers and the three bounds agree in order, which is worth saying out loud on a course where three pages show them disagreeing. What does not follow from the measurement is the shape of either curve: 45 and 14 are two numbers, and two numbers fit any pair of curves you like.