LeetCode 300 · Medium
Longest Increasing Subsequence Explained (LeetCode 300)
Updated Nov 27, 2026 · 3 min read
Keep a list called tails, where tails[i] is the smallest last number of any increasing pick of length i + 1. For each number, binary search with bisect_left for the first tail that is greater than or equal to it and replace that tail, or append if there isn't one. The length of tails is the answer, in O(n log n) time.
Key points
- ●Trying every pick is O(2^n): 256 picks for just 8 numbers.
- ●For each length, only the smallest ending number matters.
- ●bisect_left finds where each number goes in O(log n).
- ●tails is a summary, not the actual subsequence; its length is the answer.
- ●O(n log n) time, O(n) space.

The riddle: Maya's card trick
Maya lays out 8 cards in a row: 10, 9, 2, 5, 3, 7, 11, 8. You pick cards from left to right, and every card you pick has to be bigger than the last one. You can skip cards. How many can you get?
The answer is 4, for example 2, 3, 7, 11. That's LeetCode 300, Longest Increasing Subsequence: given an array of integers, return the length of the longest strictly increasing subsequence. "Subsequence" means the numbers keep their order but don't have to sit next to each other.
The slow way: try every pick
Each card is either in your pick or not, so 8 cards have 2^8 = 256 possible picks. The problem allows up to 2,500 numbers, and 2^2500 is hopeless. That's O(2^n).
A standard DP that compares each number with every earlier one gets you to O(n^2). The problem's follow-up asks for O(n log n), and the card trick gets there.
The idea: deal the cards into piles
Maya deals the cards one at a time into piles, using one rule:
- Put the card on the leftmost pile whose top card is bigger or equal.
- If no pile fits, start a new pile on the right.
When she's done, the number of piles is the answer.
Why does it work? Think of the pile tops as a list called tails. tails[i] is the smallest possible last number of an increasing pick of length i + 1. For a fixed length, a smaller ending is always at least as useful, since more future numbers can beat it. So you never need to remember every pick. You only keep the smallest tail for each length.
The tops stay sorted from left to right, which means you can find the right pile with a binary search.
Trace it on Maya's cards
| Card | What happens | tails |
|---|---|---|
| 10 | new pile | [10] |
| 9 | covers 10 | [9] |
| 2 | covers 9 | [2] |
| 5 | new pile | [2, 5] |
| 3 | covers 5 | [2, 3] |
| 7 | new pile | [2, 3, 7] |
| 11 | new pile | [2, 3, 7, 11] |
| 8 | covers 11 | [2, 3, 7, 8] |
Four piles, so the answer is 4. Step 5 of the visual sheet above deals every card the same way.
Notice the last move: 8 replaces 11. The length doesn't change, but a length-4 pick ending in 8 is easier to extend later than one ending in 11.
The code in Python
from bisect import bisect_left
def length_of_lis(nums):
tails = []
for x in nums:
i = bisect_left(tails, x)
if i == len(tails):
tails.append(x)
else:
tails[i] = x
return len(tails)
bisect_left(tails, x) returns the first position whose tail is greater than or equal to x. If that position is past the end, x beats every tail and starts a new pile. Otherwise it replaces that tail.
Run it on [10, 9, 2, 5, 3, 7, 11, 8] and it returns 4. An empty list returns 0.
Complexity
- Time: O(n log n). One pass, one binary search per number.
- Space: O(n) for the
tailslist.
From 2^n down to n log n.
The trap to watch for
Two traps, really.
Equal numbers. The pick must be strictly increasing, so [7, 7, 7] has an answer of 1. bisect_left handles it: an equal number lands on the existing pile instead of starting a new one. Swap in bisect_right and you'd count duplicates as growth.
Reading tails as the answer. tails is not always a real subsequence. It's a summary whose length is correct. Try adding a 4 to the end of Maya's cards: bisect_left([2, 3, 7, 8], 4) is 2, so 4 replaces the 7 on pile three. Now tails is [2, 3, 4, 8], which isn't a valid pick in order, but the length is still 4.
Frequently asked questions
Why use bisect_left and not bisect_right for LIS?
Because the subsequence must be strictly increasing. bisect_left puts an equal number on the existing pile, replacing a tail with the same value, so [4, 4] gives 1. bisect_right would append the second 4 and wrongly return 2.
Is the tails list the longest increasing subsequence itself?
No. It's a summary of the best ending value for each length. On [10, 9, 2, 5, 3, 7, 11, 8] it ends as [2, 3, 7, 8], which happens to be a real pick, but in general it may mix numbers from different picks. Only its length is guaranteed to be right.
What is the time complexity of Longest Increasing Subsequence?
The tails method is O(n log n) time and O(n) space: one pass over the array and one binary search per number. Checking every possible pick is O(2^n).
Is there a dynamic programming solution for LeetCode 300?
Yes, a DP that checks every earlier number for each position works in O(n^2). The tails method is faster because it keeps only the smallest tail for each length instead of every possibility.