#4 Median of Sorted Arrays

Source: Internet
Author: User

First, the 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)).

Second, the analysis

The problem is to ask for the median of two ordered sequences, and the time complexity is O (log (m+n)). Actually get this question the first idea is to merge two sequences into one, and then directly output the intermediate elements, so that two list of each element will go through, so the complexity is O (m+n). Because leetcode the data is too weak, so casually try on it, the amount. See code one.

But notice the time complexity, and "orderly" this condition, then can use binary method to do. How to use binary for a moment without an idea? To see the discuss, the basic is to find the median of the problem into the problem of small K. What do you mean? For example a=[1,2,3,4,5], the median is the third element 3, which is the 3rd small number. Find this number. Since the complexity requirement is so high, it must be impossible to merge and traverse the operation from the beginning, so the problem translates into: in the case of not merging two sequences, find out the number of median after merging . According to other people's ideas, there is the following consensus:

First assume a=[7,8,9], b = [1,2,3,5], median is 5, convert to find (3+4)/2 + 1 = 4 small number. Since the search for the small, then find a small, even the completion of a part of the task.

1, first find the median of a 8.b median of 2, found 8>2, indicating the combined median, in the left some position. In this way, we have found two relatively small numbers, for the left side of the B 2 (for each), altogether 2. The problem then translates into: Find the 2nd small number in a[7,8,9] and b[3,5].

2, repeat the above process, a median of 8,b is the median of 3,8>3, then the combined median is 8 left position. B in 3 to the left (3), a total of one, the problem into a[7,8,9] in and b[5] found in the 1th small number

3, still so find, 8>5, record 5, found that 5 is the current a[7,8,9],b[5] in the first small number, then complete, and return.

Where does the binary show up here? In the above description, binary is visually reflected in B: After the number of small statistics, the first half of the deletion, only leave the latter half, and will look for the first K small to find k-i small. Can this be reflected in a? is not 8>3, then 8 of the right side can also be deleted?

This is not possible, assuming the problem is to find the 8th small, then the first step came in 8>2, statistics 2 small, after the deletion of B into [3,5]; a very error-prone idea is to delete the left side of a, delete the right side of B, and b to [7,8], binary + binary much better. In fact, this is not right, because we found that only a small number can affect our technical results, find I, just to find k-i. And as for those big, if itself to find more k, such as k=8, to find the 8th small, in fact, is a 9, if B became [7,8], silly, can not find.

So binary and find the first k small is separate, binary find some small number, let find the K small easier some. Otherwise traverse the words, find a small, counter +1, this is O (n).

Third, Python code

1, O (m+n) method: Merge into a sequence to find the median number

1 classSolution:2     #@param {integer[]} nums13     #@param {integer[]} nums24     #@return {float}5     deffindmediansortedarrays (self, NUMS1, NUMS2):6Merge = Nums1 +NUMS27 Merge.sort ()8m =Len (nums1)9n =Len (NUMS2)TenTotal = m +N One         ifTotal% 2 = = 1: A             returnMerge[total/2] -         Else: -             returnFloat (MERGE[TOTAL/2] + MERGE[TOTAL/2-1])/2

2, O (log (m+n))

1 classSolution:2     #@param {integer[]} nums13     #@param {integer[]} nums24     #@return {float}5     deffindkth (self, S, L, K):6         #s stands for short-list, L-stands for long-list, and K-stands for the kth smallest in the list.7m =Len (s)8n =Len (l)9         Ten         #ensure the 1st list is short and the 2nd list is long One         ifM >N: A             returnself.findkth (L, S, K) -          -         #s is empty, so the kth small in l[k-1] the         if  notS: -             returnL[k-1] -              -         #the last one small number, compare s[0] and l[0] cause the list is orderd. +         ifK = = 1: -             returnmin (s[0], l[0]) +      A         #Get the mid of the current list s and L ati = min (M, K/2) -j = min (n, K/2) -      -         #The S[mid] > L[mid], cut the small in L by using l[j:], and get update the kth:k-J -         ifS[i-1] > L[j-1]: -             returnSelf.findkth (S, l[j:], K-j) in         Else: -             returnSelf.findkth (s[i:], L, K-i) to  +  -     deffindmediansortedarrays (self, NUMS1, NUMS2): theLen_1 =Len (nums1) *Len_2 =Len (NUMS2) $L = len_1 +len_2Panax Notoginsengrst = 0.0 -          the         #even and Odd +         ifL% 2 = = 1: Arst = self.findkth (Nums1, NUMS2, (L + 1)/2)/1.0 the         Else: +rst = (self.findkth (nums1, NUMS2, (L + 1)/2) + self.findkth (nums1, NUMS2, (L + 2)/2))/2.0 -          $         returnRst

Iv. Summary

1, to insist on the things in their mind to say, so that some of their own doubts have been the question written to understand.

I had been in a state of chaos until I had finished writing the report on this problem before I knew a lot. What has been not clear before, is binary how to use the problem. Although everyone said A>b, the left half of B did not have to look, I know, but just do not know why. After writing this article to know, do not see the meaning is, do not have to go to a number on the left is a few small. In fact, the above parsing part, the discussion should not remove the right half of a, is the place I have been puzzled.

2, must grasp the pain point, know good algorithm is to solve what problem.

Also just want to know the subject with binary is used to do something. Looking for the K-big, how to find? It is easy to think of the following methods:

1) from small to sort, then find a[mid], sorting complexity O (N), Look for O (1)

2) do not sort, walk directly to find, set a maximum value and counter. Find a larger than the maximum value, update the maximum, counter +1, complexity O (n)

What is the problem to be solved? To find the value of the K-large. What's the point of pain? Slow. Then why is the binary fast? Can be determined more than once. It's not going to be a knot! But not in the form, left not, as long as the right. You know, not after that, the data you don't want to go to.

3, write the idea is very useful, such as today's use of sub-functions, forget to call, written this.findkth, submitted has been error, read the problem before the note, only to find out is self.findkth, ha ~ ~

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