-
Notifications
You must be signed in to change notification settings - Fork 0
/
Median of Two Sorted Arrays
35 lines (31 loc) · 1.06 KB
/
Median of Two Sorted Arrays
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
public class Solution {
private double findMedian(int A[], int B[], int left, int right) {
int m = A.length, n = B.length, mid = (m+n)/2;
if (left > right) {
return findMedian(B, A, Math.max(0, mid-m), Math.min(n-1, mid));
}
int i = (left+right) / 2, j = mid - i - 1;
if (j >= 0 && A[i] < B[j]) // A[i] < median
return findMedian(A, B, i+1, right);
if (j < n-1 && A[i] > B[j+1]) // A[i] > median
return findMedian(A, B, left, i-1);
// found median
// m+n is odd
if ( ((m+n) & 0x1) > 0 || (i <= 0 && (j < 0 || j >= n)))
return A[i];
// m+n is even
if (j < 0 || j >= n)
return (A[i] + A[i-1]) / 2.0;
if (i <= 0)
return (A[i] + B[j]) / 2.0;
return (A[i] + Math.max(B[j], A[i-1])) / 2.0;
}
public double findMedianSortedArrays(int A[], int B[]) {
int m = A.length, n = B.length, mid = (m+n)/2;
if (m<n)
return findMedian(A, B, Math.max(0, mid-n), Math.min(m-1, mid));
else
return findMedian(B, A, Math.max(0, mid-m), Math.min(n-1, mid));
}
}
@sophie