Apex › Storage & databases

Longest increasing subsequence — patience sorting

The longest run of values that rise left to right (not necessarily adjacent), found in O(n log n) with a card-game trick. Deal the numbers like solitaire: each lands on the leftmost pile whose top is at least it, or starts a new pile — and the number of piles equals the LIS length. Pick an array and watch the piles form and the subsequence light up.

More in Storage & databases