forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 6
/
median-of-two-sorted-arrays.cpp
44 lines (38 loc) · 1.54 KB
/
median-of-two-sorted-arrays.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
// Time: O(log(min(m, n)))
// Space: O(1)
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if ((nums1.size() + nums2.size()) % 2 == 1) {
return findKthInTwoSortedArrays(nums1, nums2, (nums1.size() + nums2.size()) / 2 + 1);
} else {
return (findKthInTwoSortedArrays(nums1, nums2, (nums1.size() + nums2.size()) / 2) +
findKthInTwoSortedArrays(nums1, nums2, (nums1.size() + nums2.size()) / 2 + 1)) / 2.0;
}
}
int findKthInTwoSortedArrays(const vector<int>& A, const vector<int>& B,
int k) {
const int m = A.size();
const int n = B.size();
// Make sure m is the smaller one.
if (m > n) {
return findKthInTwoSortedArrays(B, A, k);
}
int left = 0;
int right = m;
// Find a partition of A and B
// where min left s.t. A[left] >= B[k - 1 - left]. Thus left is the (k + 1)-th element.
while (left < right) {
int mid = left + (right - left) / 2;
if (0 <= k - 1 - mid && k - 1 - mid < n && A[mid] >= B[k - 1 - mid]) {
right = mid;
} else {
left = mid + 1;
}
}
int Ai_minus_1 = left - 1 >= 0 ? A[left - 1] : numeric_limits<int>::min();
int Bj = k - 1 - left >= 0 ? B[k - 1 - left] : numeric_limits<int>::min();
// kth element would be A[left - 1] or B[k - 1 - left].
return max(Ai_minus_1, Bj);
}
};