/300. Longest Increasing Subsequence

300. Longest Increasing Subsequence

Medium
Arrays59.1% acceptance

Given an integer array sequence, return the length of the longest strictly increasing subsequence. A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.

Example 1

Input: [4, 6, 1, 2, 8, 5]

Output: 4

Explanation: The longest increasing subsequence is [1, 2, 5, 8] or [1, 2, 6, 8], so the length is 4.

Example 2

Input: [3, 2, 1]

Output: 1

Explanation: No increasing subsequence longer than 1.

Example 3

Input: [1, 2, 3, 4]

Output: 4

Explanation: The whole array is strictly increasing.

Constraints

  • 1 <= len(sequence) <= 2500
  • -104 <= sequence[i] <= 104 for all valid i
Python (current runtime)

Case 1

Input: [5, 3, 4, 8, 6, 7]

Expected: 4

Case 2

Input: [1, 1, 1, 1]

Expected: 1

Case 3

Input: [9, 8, 7, 6, 5]

Expected: 1

Case 4

Input: [2, 5, 7, 3, 6, 9, 8]

Expected: 5