Kth Smallest Element in Sorted Matrix
Master this topic with zero to advance depth.
Kth Smallest Element in Sorted Matrix
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Visual Representation
matrix =
[ 1, 5, 9 ]
[ 10, 11, 13 ]
[ 12, 13, 15 ]
k = 8
All elements sorted: [1, 5, 9, 10, 11, 12, 13, 13, 15]
The 8th smallest is 13.
Min-Heap approach (similar to Merge K sorted lists):
Initial heap: [(1,0,0)] <- (val, row, col)
Expand smallest, add right neighbor and maybe downExamples
Level I: Flatten and Sort
Intuition
The simplest approach is to collect all elements from the matrix into a flat list, sort them, and return the element at index k - 1. While not leveraging the sorted property of the matrix, this is intuitive and correct.
Detailed Dry Run
matrix = [[1,5,9],[10,11,13],[12,13,15]], k=8
- Flatten: [1, 5, 9, 10, 11, 13, 12, 13, 15]
- Sort: [1, 5, 9, 10, 11, 12, 13, 13, 15]
- Return sorted[k-1] = sorted[7] = 13
Level II: Binary Search on Value Range
Intuition
Since the matrix is sorted, any element X has a predictable number of elements smaller than or equal to it. We can binary search for the value X in the range [matrix[0][0], matrix[n-1][n-1]]. For each middle value, we count how many elements are <= mid in time using the sorted property.
Detailed Dry Run
matrix = [[1,5,9],[10,11,13],[12,13,15]], k=8 Range: [1, 15], Mid = 8 Count <= 8: (1, 5) from row 0, none from others. Count = 2. 2 < 8, search [9, 15]. Mid = 12. Count <= 12: (1,5,9), (10,11), (12). Count = 6. 6 < 8, search [13, 15]. Mid = 14. Count <= 14: All but 15. Count = 8. 8 == 8, result could be 14, search [13, 13]. Finally converge to 13.
Level III: Min-Heap (K Merged Sorted Lists)
Intuition
The matrix is essentially N sorted rows that we want to merge. We can use the same technique as "Merge K Sorted Lists": start a Min-Heap with the first element of each row. Pop the minimum, push its right neighbor. After k pops, the last popped element is the answer. Optimized: we only need to start with the first column (N elements) and expand row by row.
Detailed Dry Run
matrix = [[1,5,9],[10,11,13],[12,13,15]], k=8 Heap = [(1,r:0,c:0), (10,r:1,c:0), (12,r:2,c:0)]
- Pop (1). Push right (5,0,1). Heap=[(5,0,1),(10,1,0),(12,2,0)]. Count=1.
- Pop (5). Push (9,0,2). Count=2.
- Pop (9). No right (col 3 out of range). Count=3.
- Pop (10). Push (11,1,1). Count=4.
- Pop (11). Push (13,1,2). Count=5.
- Pop (12). Push (13,2,1). Count=6.
- Pop (13). Push (13,1,3) - out of range. Count=7.
- Pop (13). Count=8. Return 13.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.