Longest Increasing Subsequence (LIS)
Track increasing subsequences in their original order. Selected elements may be separated by unused elements.
Core Intuition & How It Works
Longest Increasing Subsequence (LIS) finds the longest strictly increasing subsequence of an array. Classic DP solves it in O(n²), while maintaining smallest ending tails with binary search (patience sorting) achieves O(n log n).
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(n²) DP, O(n log n) with binary search |
| Space Complexity | O(n) |
| Topic Chapter | Chapter 13: Dynamic Programming |
| Difficulty Level | Advanced |
Key Pattern to Recognize
In the O(n log n) method, binary search the tails array for the first element ≥ x (strictly increasing) and replace it.
Common Pitfalls & Edge Cases
Assuming the tails array itself is the actual LIS (its length is exact, but reconstructing the sequence requires predecessor pointers).
Real-World & Technical Interview Applications
Box stacking, Russian doll envelopes, diff utilities, and version dependency chains.