Longest Increasing Subsequence
Find the longest strictly increasing subsequence in O(n log n) and return its length, values and original indices.
Code
export interface LisResult {
length: number;
sequence: number[];
indices: number[];
}
export function longestIncreasingSubsequence(values: readonly number[], strict = true): LisResult {
if (!values.every((value) => Number.isFinite(value))) {
throw new TypeError("values must contain finite numbers");
}
const tails: number[] = [];
const tailIndices: number[] = [];
const previous = new Array<number>(values.length).fill(-1);
for (let index = 0; index < values.length; index += 1) {
const value = values[index]!;
let low = 0;
let high = tails.length;
while (low < high) {
const middle = (low + high) >> 1;
const goesRight = strict ? tails[middle]! < value : tails[middle]! <= value;
if (goesRight) low = middle + 1;
else high = middle;
}
tails[low] = value;
tailIndices[low] = index;
previous[index] = low > 0 ? tailIndices[low - 1]! : -1;
}
const indices: number[] = [];
let cursor = tailIndices.length > 0 ? tailIndices[tailIndices.length - 1]! : -1;
while (cursor !== -1) {
indices.push(cursor);
cursor = previous[cursor]!;
}
indices.reverse();
return {
length: indices.length,
sequence: indices.map((index) => values[index]!),
indices,
};
}How it works
Count the functions middle and longestIncreasingSubsequence as the entire surface area of Longest Increasing Subsequence.
Iteration is nested: for (let index = 0; index < values.length; index += 1) frames the pass, while (low < high) does the per-item step. Invalid states are rejected up front: it throws TypeError instead of continuing with bad data.
The result comes back as an object carrying length, indices.
Applying it
The primary signature is export function longestIncreasingSubsequence(values: readonly number[], strict = true)
longestIncreasingSubsequence is the public entry point; everything else in the file exists to support it.
The call returns length, indices — destructure what you need at the call site.
Statements that matter
throw new TypeError("values must contain finite numbers");
Interactive Tools for This Topic
FAQ
- Does `longestIncreasingSubsequence` modify its input?
- The input survives unchanged; what you get back is a newly constructed value.
- Does the code validate its input?
- Validation comes first — `TypeError` marks the failure, before any partial work happens.
- Which packages does this snippet need?
- None. Every symbol the code uses resolves inside the standard library. The file defines `middle`, `longestIncreasingSubsequence` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.