Skip to the workspace
Pebellslearn by building
Sequence DP: the Diff Engine  ›  Lesson

Longest Increasing Subsequence & the Block Reorderer

Longest Increasing Subsequence 30 minFind the longest rising run hidden in a list, first in O(n²), then in O(n log n)

dsa / Longest Increasing Subsequence & the Block Reorderer

Explore each scene at your own pace.

01 · Subsequence, not subarray

Subsequence, not subarray

Longest Increasing Subsequence & the Block Reorderer

Definition
The longest increasing subsequence of a list is the longest group of its items that rise in value while keeping their original order, though they need not sit side by side. Dynamic programming finds its length by reusing answers for shorter prefixes.
In plain words
From a scrambled row of numbered blocks, pick as many as you like, left to right, so each is bigger than the one before. You may skip blocks in between. The most you can pick are the blocks that can stay put, and the rest must move.

In short: the longest increasing subsequence (LIS) is the longest run of elements that keeps the original order but may skip some. A quadratic table, best[i] = the longest rising run ending at i, finds it in O(n²). A faster version keeps one number per length — the smallest possible ending value, the tails row — and finds the length alone in O(n log n) with binary search. The tails row is not the subsequence itself; only its length is the answer.

Four card piles, each showing a buried larger card under a smaller top card; the pile tops read 1, 2, 3, 5 left to right, which is the tails row

Quarry keeps every stored document as a list of numbered blocks, and its index expects those numbers in ascending order. After a restore from backup, the index comes back scrambled: [4, 1, 6, 2, 7, 3, 8, 5]. Rewriting a block is slow, so Quarry wants to leave as many blocks where they are as it can and move the rest. The blocks that can stay are the ones that are already in order relative to each other. The longest such group is the longest increasing subsequence, and everything outside it has to move.

This section ends with Quarry's diff engine, which shows what changed between two revisions of a document. Every lesson fills a table, starting here with the smallest: a single row.

Subsequence, not subarray

A subarray is a contiguous slice. A subsequence keeps the original order but may skip elements. In [4, 1, 6, 2, 7, 3, 8, 5], the blocks 1, 2, 3, 5 form a rising subsequence even though other blocks sit between them. They can all stay put: move the other four blocks into the gaps and the index is sorted.

The brute-force plan is to try every subsequence and keep the longest rising one. Each element is either in or out, so a list of n elements has 2ⁿ subsequences. Count just the rising ones in a short list first:

What does this print?
from itertools import combinations

blocks = [4, 1, 6, 2]
rising = 0
for size in range(1, len(blocks) + 1):
    for pick in combinations(blocks, size):
        if all(a < b for a, b in zip(pick, pick[1:])):
            rising += 1
print(rising)
Scene 1 of 10
Project context and further resources
You're building a piece ofQuarry — version history
This piece — fewest_moves(): Counts the fewest stored blocks to move so a scrambled index is back in order.
Scenario A restore from backup leaves Quarry's block index scrambled. The reorderer finds the longest group of blocks already in order, leaves them in place and moves only the rest.
fewest_moves.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
fewest_moves(order) → intThe fewest blocks to move so the index is sorted.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.