Longest Increasing Subsequence

Medium Dynamic Programming

nums is a list of whole numbers.

A subsequence of nums is some of its numbers, at least one, kept in the order they have in the list: numbers may be left out, but none moves. It is increasing when each of its numbers is larger than the one before it, so two equal numbers never follow each other in it.

Return the length of the longest increasing subsequence of nums.

Example 1

Input: nums = [3,8,4,9,5,6]
Output: 4

3, 4, 5, 6 leaves out 8 and 9 and keeps the rest in their order, each larger than the one before. Taking 8 or 9, as in 3, 8, 9, gives a shorter one, and none has 5 numbers.

Example 2

Input: nums = [4,2,2,7,1]
Output: 2

Example 3

Input: nums = [-3,10,-1,0,12,2,3]
Output: 5

Constraints

  • 1 ≤ nums.length ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹

Practice this in an AI round

30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.

See the AI round

Watch the explanation

More Dynamic Programming

All Dynamic Programming problems