Last active
October 10, 2017 17:12
-
-
Save kartikkukreja/ed1262b3b7f8161d92b975ab752c548b to your computer and use it in GitHub Desktop.
matrix-median
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int findMedian(vector<vector<int> > &A) { | |
int mn = A[0][0], mx = A[0][0], n = A.size(), m = A[0].size(); | |
for (int i = 0; i < n; ++i) { | |
if (A[i][0] < mn) mn = A[i][j]; | |
if (A[i][m-1] > mx) mx = A[i][j]; | |
} | |
int desired = (n * m + 1) / 2; | |
while (mn < mx) { | |
int mid = mn + (mx - mn) / 2; | |
int place = 0; | |
for (int i = 0; i < n; ++i) | |
place += upper_bound(A[i].begin(), A[i].end(), mid) - A[i].begin(); | |
if (place < desired) | |
mn = mid + 1; | |
else | |
mx = mid; | |
} | |
return mn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is your j value??