All topics
115

The longest chain you can keep in order

Tier 1Locked

Where it runs

1. The problem

You compare two versions of a file and want the diff to show what stayed the same. Lines were added, removed and moved around. The unchanged part is the longest set of lines found in both files in the same order, even when other lines sit between them. That is what git diff works out every day.

A related task. You get build numbers in a random order. Find the longest chain of upgrades you could apply following this list without ever going back to an older build.

Both questions have one shape: pick the longest part you can keep in order. You may skip the items between the ones you pick.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included
Longest increasing subsequence
longestIncreasingSubsequence([2, 5, 3, 7, 4, 8])
arr
2
5
3
7
4
8
dp[i]: the longest rising chain that ends at arr[i]
1
1
1
1
1
1
best = 1

Find the longest subsequence that keeps rising. Every dp[i] starts at 1, because arr[i] alone is a chain of length 1.

Speed race
Longest increasing subsequence of the same 11 numbers: every subset, a quadratic table, or patience sorting

Bars: the input numbers, never moved. A lit bar is a number being read; picking a subset lights up all its members.

One operation is one element read, or picking the next subset. Every frame, each lane does the same number of operations, so the slow one falls behind.

Every subset O(2ⁿ·n)
0 operations
Quadratic table O(n²)
0 operations
Patience sorting O(n log n)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 10
Every subset3.6K · under 1 ms
Quadratic table55 · under 1 ms
Patience sorting26.6 · under 1 ms
n = 20
Every subset3.7M · 37 ms
Quadratic table210 · under 1 ms
Patience sorting63.2 · under 1 ms
n = 40
Every subset3.8T · 11 h
Quadratic table820 · under 1 ms
Patience sorting146.4 · under 1 ms

Recognise it

DP on subsequences (LIS / LCS / edit distance)

Say these words and it is this pattern

  • longest increasing subsequence
  • longest common subsequence between two strings
  • minimum edits to turn one string into another
  • keep relative order, may skip items
  • diff or alignment between two sequences

Looks similar, isn't

One sequence of n items costs O(n²) time. Two sequences of n and m cost O(nm), and one kept row is enough.

On LeetCode

Problems that pick items in order, gaps allowed, so that each pick grows, divides or chains with the one before. For each position keep the best answer that ends there by checking every earlier position, O(n^2).

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy