Longest Subsequence
Subsequence DP where the state at index i compares against the other elements to decide how the best run extends through it. The natural recurrence is quadratic - for each index, scan the later positions and chain onto the best compatible one - but the same problem also has an O(n log n) refinement that maintains the smallest possible tail for each subsequence length via patience sorting and binary search.
300. Longest Increasing Subsequence
Medium·
4 Approaches · 2 patterns
LRU Cache
O(n^2)O(3n)
Memoization
O(n^2)O(2n)
Tabulation, O(n) Space
O(n^2)O(n)
Binary Search, O(n log n)↗ Binary Search
O(n log n)O(n)
FIG. 300 LONGEST INCREASING SUBSEQUENCE LRU● INTERACTIVE
visualization loads as you reach it