LeetCode-4. Median of Sorted Arrays

Source: Internet
Author: User

Topic:

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

Test instructions

There are two sets of sorted arrays nums1 and NUMS2, with lengths of M and N, to find the median of two sets of numbers, with the time complexity of O (log (m+n)).

The median value is: for an odd-length array such as {1,3,4}, the median value is 3, and for an even-numbered array {1,2,5,7}, the median is (2+5)/2.0=3.5.

Problem-solving ideas (borrowed from the discussion hot first answer ideas, really admire this person will algorithm problem with mathematical thinking analysis):

1. For sorted array A of length m, we can use subscript I to divide it into two groups: {a[0], ... A[i-1], | A[i], ... A[m]}.

A total of m+1 species (i = 0~m). And the left I element of the array is no more than a[i], the m-i element on the right is not less than a[i].

Similarly, a sorted array B with length n is split by J, and A and B are merged by the i + J to create an array of result.

2. To get the median value, you need to find the corresponding I and J to meet the following two conditions:

(1) i + j = m-i + n-j (M+n is an even number case)

or i + j = m-i + n-j + 1 (m+n is an odd case);

(2) All values of a[0] ~ A[i-1] and b[0] ~ B[j-1] are not all values of a[i] ~ A[m] and b[j] ~ B [n].

3. The above two conditions are equivalent to:

(1) Assuming n > m,j = (m + n + 1)/2-i;

(2) A[i-1] < B[j] and b[j-1] < a[i].

Also consider the case where the boundary value i = 0 or m, j = 0 or n is required.

4. [Coding ideas] Next, you can use the binary lookup method to help obtain the median value (the time complexity of the topic can be associated with the binary search method).

(1) Set imin = 0, Imax = m, start searching in (Imin, IMAX);

(2) Set i = (Imin + IMAX)/2, half = (m + n + 1)/2, j = half-i;

(3) if (A[i-1] > b[j]) imax = i-1; Search area narrowed to I left half

else if (A[i] < b[j-1]) Imin = i + 1; Search area narrowed to I right half

else break;

(4) if (0==i) NUM1 = b[j-1]; I==0 j = half, i.e. b[j-1] is the maximum left of the merged Result

else if (0==j) NUM1 = a[i-1]; J==0 A[i-1] is the maximum value left for Result

else NUM1 = max (b[j-1], a[i-1]); In other cases, the left maximum value of Result is taken

M+n is an odd number, return to NUM1 directly

if ((m+n)%2==1)

return NUM1;

if (m==i) num2 = B[j]; I==m, when j=0 NUM1 = A[i-1] is introduced, it is necessary to take num2 = b[j at this time)

else if (n = = j) num2 = A[i]; J==n num2 = A[i]

else num2 = min (B[j], a[i]); In other cases, take the minimum value of the right part of the Result

Return ((NUM1 + num2)/2.0);

C # code:

 Public classsolution{ Public DoubleFindmediansortedarrays (int[] nums1,int[] nums2) {        intm = nums1. Length, n =Nums2.        Length; if(M > N)returnfindmediansortedarrays (NUMS2, NUMS1); intImin =0, Imax = m, half = (m + n +1) /2; inti =0, j =0;  while(Imin <=IMAX) {i= (Imin + IMAX)/2; J= Half-i; if(I >0&& J < n && Nums1[i-1] > Nums2[j]) imax = i-1; Else if(J >0&& i < m && Nums2[j-1] > Nums1[i]) imin = i +1; Else  Break; }        DoubleNUM1 =0, num2 =0; if(0= = i) Num1 = Nums2[j-1]; Else if(0= = j) Num1 = Nums1[i-1]; ElseNUM1 = Math.max (Nums2[j-1], Nums1[i-1]); if((m + N)%2==1)            returnNUM1; if(m = = i) num2 =Nums2[j]; Else if(n = = j) num2 =Nums1[i]; Elsenum2 =math.min (Nums2[j], nums1[i]); return((NUM1 + num2)/2); }}

      

  

  

LeetCode-4. 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.