"Leeetcode" 4. Median of Sorted Arrays

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

Example 1:

NUMS1 = [1, 3]nums2 = [2]the median is 2.0

Example 2:

NUMS1 = [1, 2]NUMS2 = [3, 4]the median is (2 + 3)/2 = 2.5

Test instructions: Finding the median value of two arrays

Idea: First of all, you want to use a method like merge sorting, sorting is done, the median value is selected, but the time complexity is not O (log (m+n))

To find another way out of the web, I write down my own ideas:

Find the middle value, that is, the number of K small

The idea of using the binary lookup method, the K/2 small value in or not in a short array, such as a short array of things nums1.

If K/2 is in a short array, compare NUMS1[K/2] and NUMS2[K/2], such as if NUMS1[K/2] is smaller, then you can exclude all the numbers in Nums1 before the index K/2.

If K/2 is not in a short array, compare the values of Nums1[len (NUMS1)] and Nums2[k-len (NUMS1)], and who is smaller, exclude which array

If two values are equal, they are found!

1 classsolution (object):2     deffindmediansortedarrays (self, NUMS1, NUMS2):3         """4 : Type Nums1:list[int]5 : Type Nums2:list[int]6 : Rtype:float7         """8Len1 =Len (nums1)9Len2 =Len (NUMS2)TenL = len1 +Len2 One         ifl&0x1 = = 1: A             returnSELF.FINDK (nums1,len1,nums2,len2,l/2+1) -         Else: -             return(SELF.FINDK (NUMS1,LEN1,NUMS2,LEN2,L/2) +self.findk (nums1,len1,nums2,len2,l/2+1)) *1.0/2 the      -     deffindk (self,nums1,len1,nums2,len2,k): -         ifLen1>Len2: -             returnself.findk (nums2,len2,nums1,len1,k) +         ifLen1 = =0: -             returnNums2[k-1] +         ifK = = 1: A             returnmin (nums1[0],nums2[0]) atMID = K/2 -PA =min (mid,len1) -PB = KPA -         ifNums1[pa-1] < Nums2[pb-1]: -             returnSELF.FINDK (nums1[pa:],len1-pa,nums2,len2,k-PA) -         elifNums1[pa-1] > Nums2[pb-1]: in             returnSELF.FINDK (nums1,len1,nums2[pb:],len2-pb,k-pb) -         Else: to             returnNums1[pa-1]

"Leeetcode" 4. Median of Sorted Arrays

Related Article

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.