Menu
CoddyTech

Longest Increasing Subsequence

You get a list of integers nums. A subsequence keeps some of the elements, in their original order, and drops the rest; the kept elements do not have to sit next to each other. Return the length of the longest subsequence whose values strictly increase from left to right. Two equal values in a row do not count as increasing.

Function

lengthOfLIS(nums: integer-array) → integer
numsinteger-array
the list of integers to pick from
Returnsinteger
the length of the longest strictly increasing subsequence

Constraints

  • 1 ≤ nums.length ≤ 2500
  • -104 ≤ nums[i] ≤ 104

Examples

Input
nums = [3, 1, 8, 2, 5, 9, 4, 7]
Output
4
Explanation
Keeping 1, 2, 5, 9 gives an increasing subsequence of length 4, and so do 1, 2, 5, 7 and 1, 2, 4, 7. No choice of five values keeps rising, so the answer is 4.

lock icon+20 hidden tests on Submit

challenge icon

Follow-up

Can you return one longest increasing subsequence itself, not only its length, and still run in O(n log n) time?

Reset code
def lengthOfLIS(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [3, 1, 8, 2, 5, 9, 4, 7]

Expected

4