60.Median of Two Sorted Arrays (two sorted array median)

Level:

  Hard

Subject description:

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)).

You may assume nums1 and nums2 cannot be both empty.

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

Analysis of ideas:

  The core of this method is to find the original problem into a problem of the k-th decimal (assuming two prosequence ascending order), and the median is actually the first (m + n) / 2 small numbers. So as long to solve the problem of the decimal k, the original problem can be resolved.

  First assume that the number of array elements A and B is greater than k / 2, we compare the A [k / 2-1] and B [k / 2-1] two elements, these two elements respectively denote the k A / the first k / 2 of the light elements of the light elements 2 and B. Compare these two elements There are three cases:>, <, and =. If A [k / 2-1] <B [k / 2-1], which means that A [0] to A [k / 2-1] in front of the small elements of k elements A and B after merge . In other words, A [k / 2-1] not be greater than a small value of k after the merging of two arrays, so we can discard it.

  When A [k / 2-1]> A similar conclusion B [k / 2-1] time.

  When A [k / 2-1] = B [k / 2-1], we have found a small number of k, i.e. the equal elements, it will be referred to m. Since there are A and B, k / 2-1 elements smaller than m, i.e., m is so small numbers of k. (There may be some people doubt, if k is an odd number, then m is not the median. Idealized consideration here is slightly different in the actual code, is to find k / 2, then use kk / 2 get another number.)

  Through the above analysis, we can recursively way to achieve that is to find a small number of k-employed. In addition, we also need to consider several boundary conditions:

如果A或者B为空,则直接返回B[k-1]或者A[k-1];
如果k为1,我们只需要返回A[0]和B[0]中的较小值;
如果A[k/2-1]=B[k/2-1],返回其中一个;

Code:

public class Solution{
    public double findMedianSortedArrays(int []A,int []B){
        int m=A.length;
        int n=B.length;
        int l=(m+n+1)/2; 
        int r=(m+n+2)/2; //两个数组的长度和可能是奇数也可能是偶数
        return(getkth(A,0,B,0,l)+getkth(A,0,B,0,r))/2;
}
    public double getkth(int []A,int astart,int[]B,int bstart,int k){
        if(astart>A.length-1)
            return B[bstart+k-1];
        if(bstart>B.length-1)
            return A[astart+k-1];
        if(k==1)
            return Math.min(A[astart],B[bstart]);
        int amid=Integer.MAX_VALUE;
        int bmid=Integer.MAX_VALUE;
        if(astart+k/2-1<A.length)
            amid=A[astart+k/2-1];
        if(bstart+k/2-1<B.length)
            bmid=B[bstart+k/2-1];
        if(amid<bmid)
            return getkth(A,astart+k/2,B,bstart,k-k/2);
        else 
            return getkth(A,astart,B,bstart+k/2,k-k/2);
        
    }
}

Guess you like

Origin www.cnblogs.com/yjxyy/p/11089938.html