-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedian in row-wise sorted matrix.cpp
67 lines (62 loc) · 1.23 KB
/
Median in row-wise sorted matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Problem Statement Link - https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1
// Brute Force
// Time: O(N*M*log(N*M); Space : O(N*M)
class solution{
public:
int medianRowwiseSortedMatrix(vector<vector<int>>& matrix)
{
int m = matrix.size();
int n =matrix[0].size();
vector<int> temp(n*m);
int desired_index = (1+n*m)/2 -1;
int index = 0 ;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
tempArr[index++] = matrix[i][j];
}
}
sort(temp.begin(), temp.end());
return temp[desired_index];
}
};
// Binary Search
// Time : O(n*log(m)); Space : O(1)
class solution{
public:
int medianRowwiseSortedMatrix(vector<vector<int>>& matrix)
{
int m = matrix.size();
int n = matrix[0].size();
int min = INT_MAX;
int max = INT_MIN;
int desired_count = (1+(n*m)/2))
for(int i=0;i<m;i++)
{
if(A[i][0]<min)
{
min = A[i][0];
}
if(A[i][n-1]>max)
{
max = A[i][n-1];
}
}
int counter =0;
while(min<max)
{
counter=0;
int mid = (max+min)/2;
for(int i= 0;i<n;i++)
{
counter += upper_bound(A[i], A[i]+m, mid) - A[i];
}
if(counter < desired_count)
min = mid+1;
else
max = mid;
}
return min;
}
};