Practice · Dynamic programming (1D) · 4 of 8

Longest Increasing Subsequence

A subsequence keeps some of the items of a list, in their original order, and drops the rest: [3, 7] is a subsequence of [3, 1, 7], but [7, 3] is not.

Given a list of integers nums, return the length of its longest subsequence whose values strictly increase from left to right.

Example 1

Input
nums = [4, 10, 4, 3, 8, 9]
Output
3
[4, 8, 9] is one; [3, 8, 9] is another.

Example 2

Input
nums = [0, 8, 4, 12, 2, 10, 6, 14]
Output
4
[0, 4, 10, 14] is one of several of length 4.

Example 3

Input
nums = [5, 5, 5]
Output
1
The values must strictly increase, so equal values can't both be used.

Constraints

  • nums holds 1 to 2500 numbers.
  • −10⁴ ≤ nums[i] ≤ 10⁴