Kth Smallest Element in a Sorted Matrix – Solution & Complexity
Solution Walkthrough
Step 1: Separate matrix order from global order
- Rows and columns are sorted, but the flattened matrix is not: reading
[[1,5,9],[10,11,13],[12,13,15]]row by row gives1, 5, 9, 10, 11, 13, 12, 13, 15, so indexk - 1of that order is not the answer. - The kth smallest counts duplicates: the fully sorted order is
1, 5, 9, 10, 11, 12, 13, 13, 15, andk = 7andk = 8are both13. - The row and column structure is still enough to count how many values are
<= targetwithout examining every cell.
- Rows and columns are sorted, but the flattened matrix is not: reading
Step 2: Brute-force by flattening and sorting
- Copy every value into one array, sort it, and read the
(k - 1)th index. - This is simple and correct, but it spends
O(n^2 log n)time andO(n^2)extra space.
- Copy every value into one array, sort it, and read the
Step 3: Binary-search the answer value
- If at least
kentries are<= mid, the true answer is at mostmid. - Otherwise, the answer must be larger than
mid.
- If at least
Step 4: Count values <= mid in linear matrix time
- Start at the bottom-left corner.
- When
matrix[row][col] <= target, everything above it in that column is smaller or equal (columns are sorted), so addrow + 1and move right. Otherwise move up.
Scan for
target = 12on[[1,5,9],[10,11,13],[12,13,15]](cells marked*are counted):c0 c1 c2 r0 [ 1*, 5*, 9* ] r1 [ 10*, 11*, 13 ] r2 [ 12*, 13 , 15 ]cell value compare action count (r2, c0) 12 <= 12add 2 + 1 = 3(1, 10, 12), move right3 (r2, c1) 13 > 12move up 3 (r1, c1) 11 <= 12add 1 + 1 = 2(5, 11), move right5 (r1, c2) 13 > 12move up 5 (r0, c2) 9 <= 12add 0 + 1 = 1(9), move right6 The column index leaves the matrix, so
count(<= 12) = 6: exactly1, 5, 9, 10, 11, 12. This is themid = 12row of the binary-search trace below.Why up and right are enough
- Accepted cell: the cells above it in the same column are all
<= target, so that column is finished; move right. - Rejected cell: every cell to its right in that row is larger (rows are sorted), and rows below it were already rejected in earlier columns, so the current row contributes nothing more; move up.
- Each move either decreases
rowor increasescol, so a probe takes at most2n - 1moves:O(n)time per probe. - Duplicates need no special handling: each copy is a separate cell and is counted by
<=.
Step 5: Dry run / binary-search trace
Trace
matrix = [[1,5,9],[10,11,13],[12,13,15]],k = 8.low high mid count of values <= mid decision 1 15 8 2 need larger values, so low = 99 15 12 6 still too few, so low = 1313 15 14 8 enough values, so high = 1413 14 13 8 enough values, so high = 13When
lowmeetshigh, that value is the smallest number whose rank is at leastk. It is always a value in the matrix, because the count only changes at matrix values. Duplicates are whycount(13) = 8jumps fromcount(12) = 6: both13s occupy ranks 7 and 8.Step 6: Final solution and complexity
Binary-search the value range
[min, max] = [matrix[0][0], matrix[n-1][n-1]]and count rank inO(n)per probe. That isO(log(max - min))probes, forO(n log(max - min))time and constant extra space. The search is over values, not over then^2flattened positions, which are not in sorted order.