K-th Smallest Value in a Sorted Matrix
Problem statement
Given a non-empty rectangular integer matrix whose rows and columns are each sorted in nondecreasing order, return the k-th smallest matrix value when all values are counted with multiplicity.
Function
kthSmallest(matrix: int[][], k: int) → intExamples
Example 1
matrix = [[1,5,9],[10,11,13],[12,13,15]]k = 8return = 13The sorted values place 13 in position 8.
Example 2
matrix = [[-5]]k = 1return = -5The only value is the first smallest.
Constraints
1 <= matrix.length, matrix[i].length <= 300- Every row has the same positive length.
- Rows and columns are sorted in nondecreasing order.
1 <= k <= rows * columns- Matrix values are between
-10^9and10^9.
Source note: Interview prompt excerpt preserved as source evidence.