Blind 75
58 / 75Free practice

Longest Increasing Subsequence

Medium · Dynamic programming

Jump to code

Return the length of the longest strictly increasing subsequence of an integer array. Chosen elements keep their order but need not be adjacent.

Examples

Example 1

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

Output: 4

Example 2

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

Output: 4

Constraints

  • 1 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
Need a hint?

Keep the smallest possible tail for each achievable subsequence length.

Asked in real interviews

Candidates reported versions of Longest Increasing Subsequence from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    10,
    9,
    2,
    5,
    3,
    7,
    101,
    18
  ]
]
Expected
4
Case 2
Input
[
  [
    0,
    1,
    0,
    3,
    2,
    3
  ]
]
Expected
4
Case 3
Input
[
  [
    7,
    7,
    7,
    7,
    7,
    7,
    7
  ]
]
Expected
1
Case 4
Input
[
  [
    1,
    2,
    3,
    4
  ]
]
Expected
4
Case 5
Input
[
  [
    4,
    3,
    2,
    1
  ]
]
Expected
1

Drafts are saved per language in this browser when storage is available.