-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMedianOfTwoSortedArrays.cpp
More file actions
35 lines (31 loc) · 885 Bytes
/
Copy pathMedianOfTwoSortedArrays.cpp
File metadata and controls
35 lines (31 loc) · 885 Bytes
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
class Solution {
public:
double findMedianSortedArrays(vector<int>& a, vector<int>& b) {
if (a.size() > b.size())
return findMedianSortedArrays(b, a);
int low = 0, high = a.size();
int medianNumber = (a.size() + b.size() + 1) / 2;
bool odd = (a.size() + b.size()) % 2;
while (low <= high) {
int midx = (low + high) / 2;
int midy = medianNumber - midx;
int leftx = midx == 0 ? INT_MIN : a[midx - 1];
int rightx = midx == a.size() ? INT_MAX : a[midx];
int lefty = midy == 0 ? INT_MIN : b[midy - 1];
int righty = midy == b.size() ? INT_MAX : b[midy];
int leftMax = max(leftx, lefty);
int rightMin = min(rightx, righty);
if (leftMax <= rightMin) {
if (odd)
return leftMax;
else
return 1.0 * (leftMax + rightMin) / 2.0;
}
if (leftx > righty)
high = midx - 1;
else
low = midx + 1;
}
return -1;
}
};