Longest Increasing Subsequence Length

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write lengthOfLIS(nums): return the length of the longest strictly increasing subsequence of nums, that is, numbers picked in their original order, not necessarily next to each other, each bigger than the one before. An empty list gives 0. Leave nums unchanged. For example, lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]) is 4, from 2, 3, 7, 101. The checks include 50,000 numbers, so comparing every number with every earlier one is too slow: keep the smallest last number of an increasing run of each length, and place each new number by binary search.

Your code must pass

  • Finds the longest increasing subsequence
  • Doesn't count equal numbers as increasing
  • Handles an empty list and a single number
  • Is fast enough for 50,000 numbers