InferenceAI

Enter at least two characters to search.

Start free

Longest Rising Subsequence

mediumDynamic Programming25 min25 XP

Return the length of the longest strictly increasing subsequence of nums (elements keep their order but need not be adjacent).

Requirements

  • An O(n2)O(n^2) DP is accepted; try for O(nlog⁡n)O(n \log n) with patience sorting.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[10,9,2,5,3,7,101,18]]
TASKlongest_rising_subsequence()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [10,9,2,5,3,7,101,18]

Output: 4

Explanation: classic

Input: nums = [0,1,0,3,2,3]

Output: 4

Explanation: length four

Input: nums = [7,7,7]

Output: 1

Explanation: strictly increasing

Hint 1

Keep tails[i] = the smallest possible tail of an increasing subsequence of length i + 1.

Hint 2

Binary search where each number fits in tails.

Topics

DSADynamic ProgrammingPython