Consider the following process.
If we consider the N*M matrix as 1-D array then the median is the element of 1+N*M/2
th element.
Then consider x will be the median if x is an element of the matrix and number of matrix elements ≤ x equals 1 + N*M/2
.
As the matrix elements in each row are sorted then you can easily find the number of elements in each row less than or equals x
. For finding in the whole matrix, the complexity is N*log M
with binary search.
Then first find the minimum and maximum element from the N*M matrix. Apply Binary Search on that range and run the above function for each x.
If the number of elements in matrix ≤ x
is 1 + N*M/2
and x contains in that matrix then x
is the median.
You can consider this below C++ Code :
int median(vector<vector<int> > &A) {
int min = A[0][0], max = A[0][0];
int n = A.size(), m = A[0].size();
for (int i = 0; i < n; ++i) {
if (A[i][0] < min) min = A[i][0];
if (A[i][m-1] > max) max = A[i][m-1];
}
int element = (n * m + 1) / 2;
while (min < max) {
int mid = min + (max - min) / 2;
int cnt = 0;
for (int i = 0; i < n; ++i)
cnt += upper_bound(&A[i][0], &A[i][m], mid) - &A[i][0];
if (cnt < element)
min = mid + 1;
else
max = mid;
}
return min;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…