Leetcode | Median of two Sorted Arrays looking for the value of the K-large in 2 ordered arrays

Source: Internet
Author: User

Problem Median of Sorted Arrays
There are two sorted arrays A and B 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)).
Analysis

When it comes to a more classic generic description:
given 2 ordered arrays, find out the elements of K-large in all the elements of 2 arrays.

Idea 1

The intuitive idea is like Merge-sort, which merges 2 arrays into an array to get a value of K, but the complexity of time ( m + n span class= "Mo" id= "mathjax-span-8" style= "Font-family:mathjax_main;" >) ;

Idea 2

Then we only need the elements of the K-large, do not need to sort this complex operation: can define a counter m, indicating that the element of the large m is found, and the pointer pa,pb to the first element of the array, B, using Merge-sort, when the current element of a is less than the current element of B:pa++, m++, when *PB < *pa,pb++, m++。 Finally, when M equals K, the element of the K-large is obtained. Complexity of Time O ( k ) , but when K is close to m+n, the complexity is still O(m+n) ;

Idea 3

From the requirements of the topic O(Log (m+N)) We can think of the idea of using binary search for sure.

So is there a better plan? We can consider starting with K. If we were able to delete an element that must have been before the K-element, it would have to be K-times. But what if we can delete half of it every time? Can use a B ordered information, similar to two-point search, but also make full use of order.
Assuming that the number of elements a and B is greater than K/2, we compare the first K/2 element of a (i.e. a[k/2-1]) with the K/2 element of B (i.e., b[k/2-1]) in the following three cases (to simplify the hypothesis that K is even, the result is that K is odd):
-A[K/2-1] = = B[K/2-1];
-A[K/2-1] > B[K/2-1];
-A[K/2-1] < B[K/2-1];
If A[K/2-1] < B[K/2-1], means that the element of a[0] to A[K/2-1] must be less than the element of the A+b K-large. So you can safely delete this K/2 element in the a array;
Similarly, A[K/2-1] > B[K/2-1]; You can delete the K/2 elements in the B array;
When A[K/2-1] = = B[K/2-1], it is indicated that the element k is found, directly returning the value of A[K/2-1] or B[K/2-1].

So you can write a recursive implementation, what is the recursive termination condition?
-When a or B is empty, return directly to A[k-1] or b[k-1]
-When k = 1 o'clock, return min (a[0], b[0])//1th small represents the first element
-When A[K/2-1] = = B[K/2-1], return to A[K/2-1] or B[K/2-1]

C Language Implementation
StaticintFind_kth (intAintMintAintNintk); Staticint min(p, Q) {return(P < q)? P:q;}DoubleFindmediansortedarrays (int* Nums1,intNums1size,int* NUMS2,intNums2size) {intm = nums1size;intn = nums2size;intTotal = M+n;intK = total/2;if(Total &0x01) {returnFind_kth (Nums1, M, Nums2, N, K +1);//Odd, returns the unique intermediate value}Else{return(Find_kth (Nums1, M, Nums2, N, k) + find_kth (nums1, M, NUMS2, N, + k)1)) /2.0;//Even, returns the average of 2 in the middle}    }//Find the small value of K in Group A, B: ab[k-1]    intFind_kth (intAintMintAintNintK) {//Assuming M is less than n        if(M > N)returnFind_kth (B, N, A, M, K);if(M = =0)returnb[k-1];if(k = =1)//Termination conditions            return min(a[0], b[0]);intI_a =min(M, k/2);intI_b = k-i_a;if(a[i_a-1] < b[i_b-1])returnFind_kth (A+i_a, M-i_a, B, N, k-i_a);Else if(a[i_a-1] > b[i_b-1])returnFind_kth (A, M, B+i_b, N-i_b, K-i_b);Else            returna[i_a-1]; }

Reference: Https://github.com/soulmachine/leetcode

Leetcode | Median of two Sorted Arrays looking for the value of the K-large in 2 ordered 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.