Skip to content
Preptima
hardCodingMidSeniorStaff

Find the length of the longest strictly increasing subsequence, then get it under quadratic time.

6 min readUpdated 2026-08-15

Answer

Short answer

The quadratic dynamic programme asks for the best chain ending at each index. The faster version keeps one array where slot j holds the smallest possible tail of an increasing subsequence of length j plus one; that array stays sorted, so each element is placed by binary search for O of n log n.

Was this answer helpful?

Likely follow-ups

  • Return the subsequence itself, not just its length.
  • What single character changes if the subsequence may be non-decreasing?
  • Why does Arrays.binarySearch not give you the index you need here?
  • How many pairs of numbers can you nest inside each other, given a list of width and height pairs?

Related questions