|
| 1 | +# Kth Smallest Element in a Sorted Matrix |
| 2 | + |
| 3 | +Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kth smallest element |
| 4 | +in the matrix. |
| 5 | + |
| 6 | +Note that it is the kth smallest element in the sorted order, not the kth distinct element. |
| 7 | + |
| 8 | +You must find a solution with a memory complexity better than O(n^2). |
| 9 | + |
| 10 | +## Examples |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +Example 4: |
| 17 | + |
| 18 | +```text |
| 19 | +Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8 |
| 20 | +Output: 13 |
| 21 | +Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13 |
| 22 | +``` |
| 23 | + |
| 24 | +Example 5: |
| 25 | +```text |
| 26 | +Input: matrix = [[-5]], k = 1 |
| 27 | +Output: -5 |
| 28 | +``` |
| 29 | + |
| 30 | +## Constraints |
| 31 | + |
| 32 | +- n == `matrix.length` == `matrix[i].length` |
| 33 | +- 1 <= n <= 300 |
| 34 | +- -10^9 <= `matrix[i][j]` <= 10^9 |
| 35 | +- All the rows and columns of matrix are guaranteed to be sorted in non-decreasing order. |
| 36 | +- 1 <= k <= n^2 |
| 37 | + |
| 38 | +## Topics |
| 39 | + |
| 40 | +- Binary Search |
| 41 | +- Sorting |
| 42 | +- Heap (Priority Queue) |
| 43 | +- Matrix |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +A key observation when tackling this problem is that the matrix is sorted along rows and columns. This means that whether |
| 48 | +we look at the matrix as a collection of rows or as a collection of columns, we see a collection of sorted lists. |
| 49 | + |
| 50 | +As we know, the k way merge pattern merges k-sorted arrays into a single sorted array using a heap. Therefore, to find |
| 51 | +the `kth` smallest element in the matrix, we will use the same method where we will deal with the rows of the matrix as |
| 52 | +k sorted arrays. So, this approach uses a min-heap and inserts the first element of each matrix row into the min-heap |
| 53 | +(along with their respective row and column indexes for tracking). It then removes the top element of the heap(smallest |
| 54 | +element) and checks whether the element has any next element in its row. If it has, that element is added to the heap. |
| 55 | +This is repeated until k elements have been removed from the heap. The `kth` element removed is the `kth` smallest |
| 56 | +element of the entire matrix. |
| 57 | + |
| 58 | +Here’s how we implement our algorithm using a min-heap to find the `kth` smallest element in a sorted matrix: |
| 59 | + |
| 60 | +1. We push the first element of each row of the matrix in the min-heap, storing each element along with its row and |
| 61 | + column index. |
| 62 | +2. Remove the top (root) of the min-heap. |
| 63 | +3. If the popped element has the next element in its row, push the next element in the heap. |
| 64 | +4. Repeat steps 2 and 3 as long as there are elements in the min-heap, and stop as soon as we’ve popped k elements from |
| 65 | + it. |
| 66 | +5. The last popped element in this process is the `kth` smallest element in the matrix. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +### Time Complexity |
| 78 | + |
| 79 | +The time complexity of the first step is: |
| 80 | + |
| 81 | +- `O(min(n,k))` for iterating over whichever is the minimum of both, where n is the size of the matrix and k is the smallest |
| 82 | + element we need to find. |
| 83 | +- The push operation takes `O(log(m))` time, where m is the number of elements currently in the heap. However, since we’re |
| 84 | + adding elements only `min(n,k)` elements, therefore, the time complexity of the first loop is `O(min(n,k)×log(min(n,k)))` |
| 85 | +- In the while-loop, we pop and push `m` elements in the heap until we find the `kth` smallest element. In the worst case, |
| 86 | + the heap could have up to `min(n,k)` elements. Therefore, the time complexity of this step is `O(klog(min(n,k)))` |
| 87 | + |
| 88 | +Overall, the total time complexity of this solution is `O((min(n,k)+k)×log(min(n,k)))` |
| 89 | + |
| 90 | +### Space Complexity |
| 91 | + |
| 92 | +The space complexity is O(n), where n is the total number of elements in the min-heap. |
0 commit comments