其中 range 为最小和最大的整数之间的范围。 可以拓展到 Median of K Sorted Arrays
public class Solution {
/*
* @param A: An integer array
* @param B: An integer array
* @return: a double whose format is *.5 or *.0
*/
public double findMedianSortedArrays(int[] A, int[] B) {
int n = A.length + B.length;
if (n % 2 == 0) {
return (findKth(A, B, n / 2) + findKth(A, B, n / 2 + 1)) / 2.0;
}
return findKth(A, B, n / 2 + 1);
}
// k is not zero-based, it starts from 1
public int findKth(int[] A, int[] B, int k) {
if (A.length == 0) {
return B[k - 1];
}
if (B.length == 0) {
return A[k - 1];
}
int start = Math.min(A[0], B[0]);
int end = Math.max(A[A.length - 1], B[B.length - 1]);
// find first x that >= k numbers is smaller or equal to x
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (countSmallerOrEqual(A, mid) + countSmallerOrEqual(B, mid) < k) {
start = mid;
} else {
end = mid;
}
}
if (countSmallerOrEqual(A, start) + countSmallerOrEqual(B, start) >= k) {
return start;
}
return end;
}
private int countSmallerOrEqual(int[] arr, int number) {
int start = 0, end = arr.length - 1;
// find first index that arr[index] > number;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (arr[mid] <= number) {
start = mid;
} else {
end = mid;
}
}
if (arr[start] > number) {
return start;
}
if (arr[end] > number) {
return end;
}
return arr.length;
}
}