Longest increasing subsequence; the n log n version
The longest increasing subsequence has an O(n²) table solution and an O(n log n) version that keeps a tails array and binary searches it.
Before you start: Coin change: minimum coins and number of ways.
The idea
Pick numbers left to right, skipping freely, so each pick is bigger than the last. The longest such pick here has 4 numbers.
A subsequence may skip numbers but never reorder them. Trying every subset of n numbers means 2ⁿ picks, far too many.
Intuition
Ask a smaller question: how long is the best run that ends at each number? 7 extends the longest run ending at a smaller earlier number.
The runs ending at 5 and at 3 both have length 2, so 7 gets 2 + 1 = 3. A larger earlier number cannot come before 7 in a run.
Complexity
The table compares every pair: O(n²). Tails does one binary search per number, O(n log n), and holds at most n numbers.
The y axis is logarithmic. For 10,000 numbers that is about 50 million comparisons against about 130,000.
Pitfalls
Tails is not the subsequence itself. Here it reads 1, 3, 7, but 1 comes after 3 and 7 in the input.
Tails stores the best ending for each length. To print one real run, also store the previous index of each number.
Search for the first tail that is at least x. Searching for the first tail greater than x lets equal numbers grow the run.
With 2, 2, 2 the strict answer is 1. The wrong search appends every 2 and reports 3.
Variations
To count the longest runs, keep a second table C. C[i] adds the counts of the best earlier runs.
Russian doll envelopes: sort by width, tallest first on ties, then find the longest increasing run of heights.
Code
Pseudocode
lisTails(a):
tails ← empty list
for each x in a
p ← first index with tails[p] ≥ x (binary search)
if p = length of tails: append x
else: tails[p] ← x
return length of tails
Python
from bisect import bisect_left
def lis_tails(a):
tails = []
for x in a:
p = bisect_left(tails, x)
if p == len(tails):
tails.append(x)
else:
tails[p] = x
return len(tails)
JavaScript
function lisTails(a) {
const tails = [];
for (const x of a) {
let lo = 0, hi = tails.length;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (tails[mid] < x) lo = mid + 1;
else hi = mid;
}
if (lo === tails.length) tails.push(x);
else tails[lo] = x;
}
return tails.length;
}
Java
static int lisTails(int[] a) {
int[] tails = new int[a.length];
int size = 0;
for (int x : a) {
int lo = 0, hi = size;
while (lo < hi) {
int mid = (lo + hi) >>> 1;
if (tails[mid] < x) lo = mid + 1;
else hi = mid;
}
tails[lo] = x;
if (lo == size) size++;
}
return size;
}
Practice problems (6)
What does tails[k] hold? Warm-up
In the n log n LIS algorithm, tails is a list that is updated once per number. Pick the meaning of tails[k].
Follow the tails list Warm-up
Follow the tails list on [4, 10, 3, 8, 9]. Answer each question before the step plays.
Longest increasing subsequence Standard
Given nums, return the length of the longest strictly increasing subsequence. A subsequence keeps the original order but may skip numbers.
Longest chain of pairs Standard
Each pair [a, b] has a < b. Pair [c, d] can follow [a, b] when b < c. Return the length of the longest chain you can build. You may use the pairs in any order and may leave some out.
Number of longest increasing subsequences Standard
Given nums, return how many longest strictly increasing subsequences it has. Two subsequences differ when they use different indices, even if their values are equal.
Russian doll envelopes Challenge
Each envelope is [width, height]. One envelope fits inside another only when both its width and its height are strictly smaller. Return the largest number of envelopes that can be nested one inside the next. Rotating is not allowed.
Related topics
Next on the path: Grid DP: paths, minimum path sum, obstacles