Kth Smallest Element in a Sorted Matrix
Given an n x n matrix where each row and each column is sorted in ascending order, return the kth smallest element in the matrix by sorted order (counting duplicates), not the kth distinct element.
Open official problem prompt ↗Find the value that occupies rank k when all matrix entries are considered in sorted order.
Guessing a price in a bounded range: you name a number, an assistant tells you how many items cost that much or less, and you adjust your guess until exactly k items are at or below it.
- Input
- matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
- Output
- 13
- Why
- Sorted, the values are 1,5,9,10,11,12,13,13,15; the 8th is 13.
n == matrix.length == matrix[i].length1 <= n <= 300-10^9 <= matrix[i][j] <= 10^9All rows and all columns are sorted ascending1 <= k <= n^2