[Leetcode] 4. Median of two Sorted arrays the median of two sorted arrays after merging

Source: Internet
Author: User

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

Solution one: Regardless of the time complexity limit in O (log (m+n)), the two array is traversed once can be combined into a sequence of arrays, and then take the median of the array, time complexity O (m+n):

classSolution { Public:    DoubleFindmediansortedarrays (vector<int>& Nums1, vector<int>&nums2) {        intm =nums1.size (); intn =nums2.size (); Vector<int> VI (M + N,0); inti =0; intj =0; intK =0; if(M = =0)        {            if(n = =0)                return 0; Else                return(n%2==0) ? (Nums2[n/2-1] + nums2[n/2]) /2.0: Nums2[n/2]; }        if(n = =0)        {            if(M = =0)                return 0; Else                return(M%2==0) ? (Nums1[m/2-1] + nums1[m/2]) /2.0: Nums1[m/2]; }                if(m + N = =1)            return(M = =1) ? nums1[0]: nums2[0];  for(inti =0; I <m;) {            if(J < n && Nums1[i] >Nums2[j]) {Vi[k]=Nums2[j]; J++; K++; }            Else{Vi[k]=Nums1[i]; I++; K++; }        }         while(J <N) {vi[k]=Nums2[j]; J++; K++; }                if((m + N)%2==0)            return(vi[(m + N)/2-1] + vi[(m + N)/2]) /2.0; Else            returnvi[(m + N)/2]; }};

Solution Two: Consider the time complexity in O (log (m+n)), it is not easy to traverse two arrays from beginning to finish. Because arrays are sorted, a natural idea is to consider binary lookups. One extension to find the median of two sorted arrays is to find the K-large element of two sorted arrays, where the median is k= (m+n)/2. It can be considered that: first to find two sorted array of the K-large elements to compare, that is, compare the size of arr1[k/2-1] and arr2[k/2-1], assuming arr1[k/2-1]>arr2[k/2-1], it is obvious arr2 ranked in arr2[k/2-1 ] The previous element cannot be the median of the sorted array after merging, because arr2[k/2-1] cannot be greater than the K-large value of the merged array, and Arr2[0] to arr2[k/2-2] is less than arr2[k/2-1], then it is more unlikely to be the K-value of the merged array, So this part can be discarded, then recursively call the above procedure on an old sorted array and a new sorted array, and finally get the number of K for the sorted array after merging.

It is important to be aware that the length of the array is less than K/2 is considered separately.

classSolution { Public:    DoubleFindmediansortedarrays (vector<int>& Nums1, vector<int>&nums2) {        intm =nums1.size (); intn =nums2.size (); intt = m +N; if(M = =0|| n = =0)        {            if(M = =0&& n = =0)                return-1; if(M = =0&& n >1)            {                if(n%2==0)                    return(Nums2[n/2-1] + nums2[n/2]) /2.0; Else                    returnNums2[n/2]; }            if(n = =0&& m >1)            {                if(M%2==0)                    return(Nums1[m/2-1] + nums1[m/2]) /2.0; Else                    returnNums1[m/2]; }        }        if(T = =1)            return(M = =0) ? nums2[0]: nums1[0]; if(T = =2)            return(nums1[0] + nums2[0]) /2.0; if(t%2==0)            return(Findkth (Nums1, M, NUMS2, N, t/2) + findkth (nums1, M, NUMS2, N, t/2+1)) /2.0; Else            returnFindkth (Nums1, M, NUMS2, N, t/2+1); }Private:    DoubleFindkth (vector<int> A,int  as, vector<int> B,intBsintk) {if( as>BS)returnFindkth (b, BS, A, as, K); if(k <=0)            return-1; if( as==0|| BS = =0)        {            if( as==0&& bs = =0)                return-1; if( as==0)                returnB[k-1]; if(bs = =0)                returnA[k-1]; }        if(k = =1)            returna[0] > b[0] ? b[0]: a[0]; intPA = k/2> as? asK2; intPB = K-PA; if(A[pa-1] > B[PB-1])            returnFindkth (A, as, vector<int> (B.begin () + PB, B.end ()), BS-PB, K-pb); Else if(A[pa-1] < B[PB-1])            returnFindkth (vector<int> (A.begin () + PA, a.end ()), as-PA, B, BS, K-PA); Else            returnA[PA-1]; }};

[Leetcode] 4. Median of two Sorted arrays the median of two sorted arrays after merging

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.