#300

Longest Increasing Subsequence

Medium Patience sorting + binary search

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the length of the longest strictly increasing subsequence.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the longest strictly increasing subsequence.

Why this works: tails[k] = the smallest possible tail of an increasing run of length k + 1. Each number replaces the first tail ≥ it (binary search).

Example: [10, 9, 2, 5, 3, 7, 101, 18]

tails: [10] → [9] → [2] → [2,5] → [2,3] → [2,3,7] → [2,3,7,101] → [2,3,7,18]length 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Thinking tails is an actual LIS — only its length is meaningful.

Solve on LeetCode ↗
Complexity
  • Time: O(n log n).
  • Space: O(n).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon