4. median of two sorted arrays


There are two sorted arrays nums1 and nums2 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 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

题目描述:
给出两个已经排好序的数组nums1和nums2,找到它们的中位数
Solution 1:
解决方法首先想到的是合并数组,合并之后排序,然后分数组的长度奇偶找中位数
Solution 2:
Assume that the number of elements in A and B are both larger than k/2, and if we compare the k/2-th smallest element in A(i.e. A[k/2-1]) and the k-th smallest element in B(i.e. B[k/2 - 1]), there are three results:
(Becasue k can be odd or even number, so we assume k is even number here for simplicy. The following is also true when k is an odd number.)
A[k/2-1] = B[k/2-1]
A[k/2-1] > B[k/2-1]
A[k/2-1] < B[k/2-1]
if A[k/2-1] < B[k/2-1], that means all the elements from A[0] to Ak/2-1 are in the range of k smallest elements in the union of A and B. Or, in the other word, A[k/2 - 1] can never be larger than the k-th smalleset element in the union of A and B.
Why?
We can use a proof by contradiction. Since A[k/2 - 1] is larger than the k-th smallest element in the union of A and B, then we assume it is the (k+1)-th smallest one. Since it is smaller than B[k/2 - 1], then B[k/2 - 1] should be at least the (k+2)-th smallest one. So there are at most (k/2-1) elements smaller than A[k/2-1] in A, and at most (k/2 - 1) elements smaller than A[k/2-1] in B.So the total number is k/2+k/2-2, which, no matter when k is odd or even, is surly smaller than k(since A[k/2-1] is the (k+1)-th smallest element). So A[k/2-1] can never larger than the k-th smallest element in the union of A and B if A[k/2-1] B[k/2-1] condition, which we should drop the elements in B.
When A[k/2-1] = B[k/2-1], then we have found the k-th smallest element, that is the equal element, we can call it m. There are each (k/2-1) numbers smaller than m in A and B, so m must be the k-th smallest number. So we can call a function recursively, when A[k/2-1] < B[k/2-1], we drop the elements in A, else we drop the elements in B.
We should also consider the edge case, that is, when should we stop?
1. When A or B is empty, we return Bk-1, respectively;
2. When k is 1(when A and B are both not empty), we return the smaller one of A[0] and B[0]
3. When A[k/2-1] = B[k/2-1], we should return one of them
In the code, we check if m is larger than n to garentee that the we always know the smaller array, for coding simplicy.

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public class {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] add1 = Arrays.copyOf(nums1, nums1.length + nums2.length);
System.arraycopy(nums2, 0, add1, nums1.length, nums2.length);
Arrays.sort(add1);
int l=add1.length;
if(l%2==0){
double m=0.5*(add1[l/2-1]+add1[l/2]);
return m;
}
else{
double m=(double)add1[(l-1)/2];
return m;
}

}
}

Solution 2:

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

public static double findMedianSortedArrays(int A[], int B[]) {
int m = A.length;
int n = B.length;
int total = m+n;
if (total%2 != 0)
return (double) findKth(A, 0, m-1, B, 0, n-1, total/2+1);
else {
double x = findKth(A, 0, m-1, B, 0, n-1, total/2);
double y = findKth(A, 0, m-1, B, 0, n-1, total/2+1);
return (double)(x+y)/2;
}
}

public static int findKth(int[] A, int astart, int aend, int[] B, int bstart, int bend, int k) {
int m = aend - astart + 1;
int n = bend - bstart + 1;

if(m>n)
return findKth(B,bstart,bend,A,astart,aend,k);
if(m==0)
return B[k-1];
if(k==1)
return Math.min(A[astart],B[bstart]);

int partA = Math.min(k/2,m);
int partB = k - partA;
if(A[astart+partA-1] < B[bstart+partB-1])
return findKth(A,astart+partA,aend,B,bstart,bend,k-partA);
else if(A[astart+partA-1] > B[bstart+partB-1])
return findKth(A,astart,aend,B,bstart+partB,bend,k-partB);
else
return A[astart+partA-1];
}