longestIncreasingSubsequence

Calculate a longest increasing subsequence of sequence. This subsequence is not necessarily contiguous, or unique. Given a sequence of n elements the algorithm uses O(n log n) evaluation of pred.

longestIncreasingSubsequence
(
alias pred = "a < b"
Range
)
(
Range sequence
)
if (
isRandomAccessRange!Range
)

Examples

Example from Wikipedia

import std.algorithm : equal;

auto inputSequence = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15];
auto expectedOutput = [0, 2, 6, 9, 11, 15];

assert(inputSequence.longestIncreasingSubsequence.equal(expectedOutput));

Example using a different pred

import std.algorithm : equal;
import std.range : retro;

auto inputSequence = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15];
auto expectedOutput = [12, 10, 9, 5, 3];

assert(inputSequence.longestIncreasingSubsequence!"a > b".equal(expectedOutput));

See Also

Meta