Leetcode:median of Sorted Arrays

Source: Internet
Author: User

Title Requirements:

There is sorted arrays A and B of size m and N respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).

Title Address: https://leetcode.com/problems/median-of-two-sorted-arrays/

 Public classSolution {Private Static intFind_kth (int[] A,int[] B,intk) {        //Arrays.copyofrange (original, from, to)        if(a.length>0) System.out.println ("Current A (" +a.length+ "):" +a[0]+ "--" +a[a.length-1]); if(b.length>0) System.out.println ("Current B (" +b.length+ "):" +b[0]+ "--" +b[b.length-1]); System.out.println ("Current K:" +k); if(a.length>b.length)returnfind_kth (b,a,k); if(a.length==0)returnB[k-1]; if(k==1)return(A[0]<b[0]? A[0]:b[0]); intIa= (K/2) >= (a.length)? A.length: (K/2)); intib=k-IA; System.out.println ("+a[ia-1]+" B in contrast a "+b[ib-1]); if(a[ia-1]==b[ib-1]){            returnA[ia-1]; }        Else if(a[ia-1]<b[ib-1]){            int[] Temp_a=Arrays.copyofrange (A, IA, a.length); int[] Temp_b=arrays.copyofrange (B, 0, IB); System.out.print ("Eliminate the" +a[0]+ "--" +a[ia-1]); if(ib<b.length) System.out.println ("; B "+b[ib]+"-"+b[b.length-1]); returnFind_kth (temp_a,temp_b,k-IA); }Else if(a[ia-1]>b[ib-1]){            int[] temp_b=Arrays.copyofrange (B, IB, b.length); int[] Temp_a=arrays.copyofrange (A, 0, IA); if(ia<a.length) System.out.print ("+a[ia]+" in "a" and "+a[a.length-1"]); System.out.println ("; B "+b[0]+"-"+b[ib-1]); returnFind_kth (temp_a,temp_b,k-IB); }        return0; }     Public Static DoubleFindmediansortedarrays (intA[],intb[]) {       intm=a.length,n=b.length; inttotal=m+O; if(total%2==0){           //System.out.println ("GG");           return(Find_kth (A,B,TOTAL/2) +find_kth (a,b,total/2+1))/2.0; }Else{           return(Double) find_kth (a,b,total/2+1); }            }}

The problem can be generalized to find the 2 ordered array (A, b) of the K-large elements, you can find a K/2 large element and the K/2 large element of a comparison, assuming that K/2 is an integer, there are the following 3 cases:

AK/2>BK/2: Then there must be BK/2 itself and the preceding element is not the K-large, and the elements behind the AK/2 do not include AK/2 must not be the largest, can be excluded.

But the algorithm does not need K/2 to be an integer, only uses the ia+ib=k to be able.

Leetcode:median of Sorted Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.