There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example
For A = [1,2,3,4,5,6] B = [2,3,4,5], the median is 3.5
For A = [1,2,3] B = [4,5], the median is 3
Challenge
Time Complexity O(logn)
何谓"Median"? 由题目意思可得即为两个数组中一半数据比它大,另一半数据比它小的那个数。详见 中位数 - 维基百科,自由的百科全书,题中已有信息两个数组均为有序,题目要求时间复杂度为O(log),因此应该往二分法上想。
在两个数组中找第k大数->找中位数即为找第k大数的一个特殊情况——第(A.length + B.length) / 2 大数。因此首先需要解决找第k大数的问题。这个联想确实有点牵强...
使用归并的思想逐个比较找出中位数的复杂度为O(n),显然不符要求,接下来考虑使用二分法。由于是找第k大数,使用二分法则比较A[k/2 - 1]和B[k/2 - 1],并思考这两个元素和第k大元素的关系。
class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
if (A.empty() && B.empty()) {
return 0;
}
vector<int> NonEmpty;
if (A.empty()) {
NonEmpty = B;
}
if (B.empty()) {
NonEmpty = A;
}
if (!NonEmpty.empty()) {
vector<int>::size_type len_vec = NonEmpty.size();
return len_vec % 2 == 0 ?
(NonEmpty[len_vec / 2 - 1] + NonEmpty[len_vec / 2]) / 2.0 :
NonEmpty[len_vec / 2];
}
vector<int>::size_type len = A.size() + B.size();
if (len % 2 == 0) {
return ((findKth(A, 0, B, 0, len / 2) + findKth(A, 0, B, 0, len / 2 + 1)) / 2.0);
} else {
return findKth(A, 0, B, 0, len / 2 + 1);
}
// write your code here
}
private:
int findKth(vector<int> &A, vector<int>::size_type A_start, vector<int> &B, vector<int>::size_type B_start, int k) {
if (A_start > A.size() - 1) {
// all of the element of A are smaller than the kTh number
return B[B_start + k - 1];
}
if (B_start > B.size() - 1) {
// all of the element of B are smaller than the kTh number
return A[A_start + k - 1];
}
if (k == 1) {
return A[A_start] < B[B_start] ? A[A_start] : B[B_start];
}
int A_key = A_start + k / 2 - 1 < A.size() ?
A[A_start + k / 2 - 1] : INT_MAX;
int B_key = B_start + k / 2 - 1 < B.size() ?
B[B_start + k / 2 - 1] : INT_MAX;
if (A_key > B_key) {
return findKth(A, A_start, B, B_start + k / 2, k - k / 2);
} else {
return findKth(A, A_start + k / 2, B, B_start, k - k / 2);
}
}
};
此题的边界条件较多,不容易直接从代码看清思路。首先分析找k大的辅助程序。
A_start > A.size() - 1
,意味着A中无数提供,故仅能从B中取,所以只能是B从B_start
开始的第k个数。下面的B...分析方法类似。A_start
开始的第k / 2
个数,若下标A_start + k / 2 - 1 < A.size()
,则可取此下标对应的元素,否则置为int的最大值,便于后面进行比较,免去了诸多边界条件的判断。A_key > B_key
,取小的折半递归调用findKth。接下来分析findMedianSortedArrays
:
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) {
// write your code here
if (A.length == 0 && B.length == 0) {
return 0;
}
int len = A.length + B.length;
if (len % 2 == 0) {
return (findKth(A, 0, B, 0, len/2) + findKth(A, 0, B, 0, len/2+1)) / 2.0;
} else {
return findKth(A, 0, B, 0, len/2 + 1);
}
}
//find kth number of two sorted array
public static int findKth(int[] A, int A_start, int[] B, int B_start, int k) {
if (A_start >= A.length) {
return B[B_start + k - 1];
}
if (B_start >= B.length) {
return A[A_start + k - 1];
}
if (k == 1) {
return Math.min(A[A_start], B[B_start]);
}
int A_key = (A_start + k/2 - 1 < A.length) // if one array is too short
? A[A_start + k/2 - 1] : Integer.MAX_VALUE; // trick
int B_key = (B_start + k/2 - 1 < B.length) // if one array is too short
? B[B_start + k/2 - 1] : Integer.MAX_VALUE; // trick
if (A_key < B_key) {
return findKth(A, A_start + k/2, B, B_start, k - k/2);
} else {
return findKth(A, A_start, B, B_start + k/2, k - k/2);
}
}
}
[]A, []B, k
谁先达到极端情况。[]A, []B
中某一个数组太短了,无法取k/2,则返回无穷大,设置了Integer.MAX_VALUE。