378. Kth Smallest Element in a Sorted Matrix
Practice Kth Smallest Element in a Sorted Matrix for free on NudgeCode: Medium, Heap pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
matrix is an n x n grid where every row and every column is sorted in non-decreasing order. Return the k-th smallest value among all n^2 entries, counting duplicates: if the sorted list of all entries is v, return v[k - 1].
Try to use less than O(n^2) extra memory.
Example
matrix = [[1,5,9],
[10,11,13],
[12,13,15]], k = 8 -> 13
All entries in order are 1,5,9,10,11,12,13,13,15; the 8th is 13.
Constraints
- 1 <= n <= 300
- -10^9 <= matrix[i][j] <= 10^9
- Every row and every column is sorted in non-decreasing order
- 1 <= k <= n^2