Longest Rising Subsequence

HardDynamic Programming≈ 45 min

A subsequence of a list keeps some of its numbers, in their original order, and drops the rest; the numbers kept do not have to be next to each other. A subsequence is rising when each number in it is strictly larger than the one before.

Given a list of integers nums, return the length of its longest rising subsequence.

Examples

text
nums = [10, 9, 2, 5, 3, 7, 101, 18]   →  4
nums = [0, 1, 0, 3, 2, 3]             →  4
nums = [7, 7, 7, 7]                   →  1
nums = [5, 4, 3, 2, 1]                →  1

In the first example 2, 3, 7, 18 (or 2, 5, 7, 101) rises all the way. In the third, equal numbers do not rise.

Constraints

  • Between 0 and 20,000 numbers, each between -1,000,000,000 and 1,000,000,000.
  • Aim for O(n log n): the table that compares every number with every earlier one is O(n²).
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.