您的位置:首页 > 其它

4. Median of Two Sorted Arrays

2016-04-18 12:00 211 查看
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)).

The first solution could be generalized to Find the Kth value in 2 sorted arrays.

Solution 1. Divide and Conquer, Recursion
public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
int m = A.length;
int n = B.length;
int total = m+n;
if((total & 0x1)==1) {
return findKth(A, 0, m, B, 0, n, total/2+1);
} else {
return (findKth(A, 0, m, B, 0, n, total/2)+findKth(A, 0, m, B, 0, n, total/2+1))/2.0;
}
}
public double findKth(int[] A, int s1, int t1, int[] B, int s2, int t2, int K) {
if(t1-s1>t2-s2) return findKth(B, s2, t2, A, s1, t1, K);
if(t1-s1==0) return B[s2+K-1];
if(K == 1) return Math.min(A[s1], B[s2]);

int anum = Math.min(K/2+s1, t1);
int bnum = K - anum + s1 + s2;
if(A[anum-1]>B[bnum-1]) {
return findKth(A, s1, anum, B, bnum, t2, K-bnum+s2);
} else {
return findKth(A, anum, t1, B, s2, bnum, K-anum+s1);
}
}
}


The second solution convert the problem to make it clear as a Divide and Conquer problem -- "Find a specific value satisfied some conditions in an array"

Refer to https://leetcode.com/discuss/15790/share-my-o-log-min-m-n-solution-with-explanation
To solve this problem, we need to understand "What is the use of median". In statistics, the median is used for dividing a set into two equal length subsets, that one subset is always greater than the other. If we understand the use of median for dividing, we are very close to the answer.

First let's cut A into two parts at a random position i:

left_A             |        right_A
A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]
Since A has m elements, so there are m+1 kinds of cutting( i = 0 ~ m ). And we know: len(left_A) = i, len(right_A) = m - i . Note: when i = 0 , left_A is empty, and when i = m , right_A is empty.

With the same way, cut B into two parts at a random position j:

left_B             |        right_B
B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]
Put left_A and left_B into one set, and put right_A and right_B into another set. Let's name them left_part and right_part :

left_part          |        right_part
A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]
B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]
If we can ensure:

1) len(left_part) == len(right_part)
2) max(left_part) <= min(right_part)
then we divide all elements in {A, B} into two parts with equal length, and one part is always greater than the other. Then median = (max(left_part) + min(right_part))/2.

To ensure these two conditions, we just need to ensure:

(1) i + j == m - i + n - j (or: m - i + n - j + 1)
if n >= m, we just need to set: i = 0 ~ m, j = (m + n + 1)/2 - i
(2) B[j-1] <= A[i] and A[i-1] <= B[j]
(For simplicity, I presume A[i-1],B[j-1],A[i],B[j] are always valid even if i=0/i=m/j=0/j=n . I will talk about how to deal with these edge values at last.)

So, all we need to do is:

Searching i in [0, m], to find an object `i` that:
B[j-1] <= A[i] and A[i-1] <= B[j], ( where j = (m + n + 1)/2 - i )


public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1);

int m = nums1.length, n = nums2.length;
int K = (m + n + 1) / 2;

double ret1 = 0.0, ret2 = 0.0;
int amin = 0, amax = nums1.length;

// include amin, exclude amax, when nums1.length==0, this still work
while (amin <= amax) {
int aidx = (amax - amin) / 2 + amin;
int bidx = K - aidx;
if (aidx > 0 && bidx < n && nums1[aidx-1] > nums2[bidx]) {
// in case m == n and aidx == 0 --> bidx == n
amax = aidx-1;
} else if (bidx > 0 && aidx < m && nums1[aidx] < nums2[bidx-1]) {
// in case m == n and aidx == m --> bidx == 0
amin = aidx+1;
} else {
// nums1[aidx] > nums2[bidx-1] && nums2[bidx] > nums1[aidx-1]
if (aidx == 0) {
ret1 = (double) nums2[bidx-1];
} else if (bidx == 0) {
ret1 = (double) nums1[aidx-1];
} else {
ret1 = (double) Math.max(nums1[aidx-1], nums2[bidx-1]);
}

if (((m + n) & 0x1) == 0) {
if (aidx == m) {
ret2 = (double) nums2[bidx];
} else if (bidx == n) {
ret2 = (double) nums1[aidx];
} else {
ret2 = (double) Math.min(nums1[aidx], nums2[bidx]);
}
} else {
ret2 = ret1;
}

break;
}
}

return (ret1 + ret2) * 0.5;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: