Leetcode's Featured Problem archive

Source: Internet
Author: User

002* Median of the Sorted Arrays

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

1 classSolution:2     deffindkth (self, A, A_start, B, B_start, K):3         ifA_start >=Len (A):4             returnB[b_start + k-1]5         ifB_start >=Len (B):6             returnA[a_start + k-1]7         ifK = = 1:8             returnmin (A[a_start], B[b_start])9A_key = 0x3f3f3f3fTen         ifA_start + K/2-1 <Len (A): OneA_key = A[a_start + K/2-1] AB_key = 0x3f3f3f3f -         ifB_start + K/2-1 <Len (B): -B_key = B[b_start + K/2-1] the         ifA_key <B_key: -             returnSelf.findkth (A, A_start + K/2, B, B_start, K-k/2) -         Else: -             returnSelf.findkth (A, A_start, B, B_start + K/2, K-k/2) +     #@return A float -     deffindmediansortedarrays (self, A, B): +n = Len (A) +Len (B) A         ifn% 2 = =0: at             return(Self.findkth (A, 0, b, 0, N/2) + self.findkth (A, 0, b, 0, N/2 + 1)/2.0 -         Else: -             returnSelf.findkth (A, 0, B, 0, N/2 + 1)
View Code

Introduction to the algorithm 9.3-8, set X[1..N] and Y[1..N] is two arrays, each containing n ordered number. An O (LGN) time algorithm is given for calculating the median of all 2n elements in the array x and Y.

1 intFindmedian (intA[],intB[],intNintLowintHigh ) {  2     if(Low > High)returnNot_found; 3     Else {  4         intK = (Low+high)/2; 5         if(K==n && a[n]<=b[1])returnA[n]; 6         Else if(K<n && b[n-k]<=a[k] && a[k]<=b[n-k+1])returnA[k]; 7         Else if(A[k] > b[n-k+1])returnFindmedian (a,b,n,low,k-1); 8         Else returnFindmedian (a,b,n,k+1, high); 9     }  Ten }   One intTwoarraymedian (intX[],intY[],intN) { A     intMedian = Findmedian (X,y,n,1, N);  -     if(median==not_found) median = Findmedian (Y,x,n,1, N);  -     returnmedian;  the}
View Code

We're going to take the median, and notice that a number x is the median, and if and only if the number of n-1 is smaller than x, and there is a number of n larger than X, we first assume that the median is in array a, where the median number of squares may be.
The median interval is assumed to be [l,r],k = (l+r)/2. If A[K] is the median, the number of k-1 in array A is smaller than a[k], and the n-k number is greater than a[k]. If the number of n-k in B is smaller than a[k], there is a number of k greater than a[k], then a[k] is the median.
Because B is ordered, so b[n-k]<=a[k]<=b[n-k+1].
If a[k]>b[n-k+1], then a number smaller than a[k] is at least N, so the median is less than a[k], so it is in the interval [l,k-1].
The inverse is in the interval [k+1,r].
If the l>r, then the median is not in a, the B array for the same two-point operation can be.

Leetcode's Featured Problem archive

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.