Longest Increasing Subsequence
O(n log n)
O(n²) dp[i] = best ending at i, or O(n log n) patience: binary-search each value into tails[].
0/10 solved
Recognise it
The signal
Longest increasing or chain subsequence in one array (increasing, divisible, nested envelopes), or min deletions to make it sorted.
1Brute force
Enumerate subsequences.
2The observation
O(n2) pair transitions: tails is sorted so binary search finds the extension point.
3Why it works
tailsis strictly increasing andtails[k]is the smallest tail of any increasing subsequence of length k+1 so far.
Reach for it when
- +Longest increasing / chain subsequence in a single ordered array.
- +2-D nesting ('Russian doll', pairs) → sort by one dim, LIS on the other.
- +Counting LIS, or chains by a custom relation (divisibility).
Not when
- ×You're comparing two sequencesuse LCS-family 2-D DP
- ×You need a contiguous increasing run (subarray, not subsequence)use Linear scan
- ×Chaining intervalsuse Greedy by end time, simpler
- ×You need to count LIS in O(n log n)use BIT/segment tree over values
Intuition
Longest Increasing Subsequence: from one array, pick elements (keeping order, allowed to skip) so they strictly increase, as long as possible. [10,9,2,5,3,7,101,18] → [2,3,7,101], length 4.
Read the full intuition
The O(n²) way (understand this first): dp[i] = length of the longest increasing subsequence ending exactly at i. For each i, look back at every j < i; if a[j] < a[i], you can extend that subsequence → dp[i] = max(dp[i], dp[j] + 1). Answer = max over all dp[i]. Simple, always correct, great default.
The O(n log n) way (patience sorting): keep an array tails, where tails[k] = the smallest possible tail of an increasing subsequence of length k+1. For each number, binary-search the first tail ≥ it and overwrite it (or append if it's bigger than all). The length of tails is the LIS length. You're greedily keeping every length's tail as small as possible so future numbers have the best chance to extend it.
Walk [2,5,3,7]: tails [] → [2] → [2,5] → 3 replaces 5 → [2,3] → 7 appends → [2,3,7]. Length 3. (Note: tails isn't the actual subsequence, just its length is correct.)
Key idea
Tells: 'longest increasing/chain subsequence in one ordered array', often with a twist — count them, a 2-D 'envelope/doll' version (sort one dimension, LIS the other), or 'largest divisible subset' (sort, then LIS by divisibility).
Why it costs what it costs
Time O(n²) or O(n log n). the dp version checks all pairs; the tails version does one binary search per element.
Space O(n). the dp or tails array of size up to n.
Practice
- Longest Increasing SubsequenceFoundationFoundationAmazon, MicrosoftMedium
- Increasing SubsequenceApply it · n = 2e5 forces n log n.Apply itCSESMedium
- Largest Divisible SubsetOne change · Relation is divisibility.One changeMedium
- Longest String ChainOne change · Relation is 'one letter added'.One changeMedium
- Maximum Length of Pair ChainTwists · Looks like LIS.TwistsMedium
- Number of Longest Increasing SubsequenceBranches · Count LIS.BranchesMedium
- Russian Doll EnvelopesAdvanced · Two dimensions.AdvancedHard
- Increasing Subsequence IIMastery checkMastery checkCSESHard
- Permutation SubsequenceCSESHard
- Sorting MethodsCSESMedium
Pitfalls
Classic bugs
- !Strict vs non-strict:
bisect_left/lower_boundfor strictly increasing,bisect_right/upper_boundto allow equals. - !Returned
tailsas the subsequence: it gives only the length. Track parent indices to rebuild the actual LIS. - !Forgot the empty array: return 0.
- !Russian dolls: sort width asc, and on equal width height desc so equal widths can't chain; then LIS on heights.
- !Counting LIS (O(n²)) needs a parallel
count[i]: reset it whendp[j]+1beatsdp[i], add to it on a tie.
Looks like it, isn't
- Maximum Length of Pair Chain needs Interval Scheduling (sort by end)
Why
LIS DP works in O(n2) but sorting by end and greedy is optimal in O(n log n). - Sequence needs Slope Trick
Why
Keeping a longest non-decreasing subsequence is wrong (cost is total |change|). Use DP over values, or the slope-trick heap. source ↗
Go deepervariants and techniques · 2 combinations · resources
Variants and techniques
- LIS length (DP)dp[i]=max(dp[j]+1) for a[j]<a[i]. Longest Increasing Subsequence.
- LIS length (patience)Binary-search into tails; length = tails size. LIS in O(n log n).
- Count of LISTrack count[i] alongside dp[i]. Number of LIS.
- Chain by relationSort, then LIS using divisibility / interval order. Largest Divisible Subset, Maximum Length of Pair Chain.
- 2-D nestingSort by one dimension (ties descending), LIS on the other. Russian Doll Envelopes.
Combinations
Longest Increasing Subsequence+Binary Search
longest chain with n large
- Longest Increasing Subsequence:
- chain formulation
- Binary Search:
- finds the tail to replace
Longest Increasing SubsequenceRussian Doll EnvelopesIncreasing Subsequence
Longest Increasing Subsequence+Segment Tree / Fenwick (BIT)+Coordinate Compression
count increasing subsequences; LIS with weights
- Longest Increasing Subsequence:
- dp over chains ending at each value
- Segment Tree / Fenwick (BIT):
- prefix max/sum over smaller values
- Coordinate Compression:
- value ranks