Given a sorted matrix of integers A of size N x M and an integer B.
Each of the rows and columns of matrix A is sorted in ascending order, find the Bth smallest element in the matrix.
NOTE: Return The Bth smallest element in the sorted order, not the Bth distinct element.
Problem Constraints
1 <= N, M <= 500
1 <= A[i] <= 109
1 <= B <= N * M
Input Format
The first argument given is the integer matrix A.
The second argument given is an integer B.
Output Format
Return the B-th smallest element in the matrix.
Example Input
Input 1:
A = [ [9, 11, 15],
[10, 15, 17] ]
B = 6
Output 1:
17
Explanation 1:
6th smallest element in the sorted matrix is 17.
Input 2:
A = [ [5, 9, 11],
[9, 11, 13],
[10, 12, 15],
[13, 14, 16],
[16, 20, 21] ]
B = 12
Output 2:
16
Explanation 2:
12th smallest element in the sorted matrix is 16.
solve(A,B)
Initialization:
low
to the minimum element in the matrix (top-left corner).high
to the maximum element in the matrix (bottom-right corner).Binary Search Loop:
low
is no longer less than high
.mid
value as the midpoint between low
and high
.Count Elements Less than or Equal to mid:
countLessEqual
function to count how many elements in the matrix are less than or equal to mid
.Adjust Search Space:
B
, it means that the Bth smallest element is in the right half of the current search space. Therefore, update low = mid + 1
.B
, it means that the Bth smallest element is in the left half of the current search space. Therefore, update high = mid
.Exit the Loop:
low
is no longer less than high
.Return Result:
low
, which represents the Bth smallest element in the matrix.countLessEqual(matrix, mid)
The countLessEqual
function essentially simulates the process of finding the count of elements less than or equal to a given value (mid
) in a partially sorted matrix. This count is then used in the binary search algorithm to adjust the search space.
count
to 0
, row
to the last row index, and col
to 0
.matrix[row][col]
) is less than or equal to mid
:
count
by the number of elements in the current column (up to the current row).col++
).mid
:
row--
).The countLessEqual
function essentially simulates the process of finding the count of elements less than or equal to a given value (mid
) in a partially sorted matrix. This count is then used in the binary search algorithm to adjust the search space.
solve: function (A, B) {
const rows = A.length;
const cols = A[0].length;
let low = A[0][0];
let high = A[rows - 1][cols - 1];
while (low < high) {
const mid = low + Math.floor((high - low) / 2);
const count = countLessEqual(A, mid);
if (count < B) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
function countLessEqual(matrix, mid) {
let count = 0;
const rows = matrix.length;
const cols = matrix[0].length;
let row = rows - 1;
let col = 0;
while (row >= 0 && col < cols) {
// If matrix[row][col] is less than or equal to mid,
// then all elements in the current column (up to the row)
// are less than or equal to mid.
if (matrix[row][col] <= mid) {
count += row + 1;
col++;
} else {
// If matrix[row][col] is greater than mid,
// move to the previous row in the same column.
row--;
}
}
return count;
}