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
1, 2, 5, 9 rises, and so do 1, 2, 5, 7 and 1, 2, 4, 7: length 4. No choice of five values keeps rising.
challenge icon

Follow-up

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

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

Test #1

Test #2

Test #3

Argument

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

Expected Output

4