Algorithm 6-----find the median after merging two sorted arrays

1. Subject:

Given two sorted arrays  nums1  and  nums2  of size m and n .

Find the median of these two sorted arrays. The time complexity of the algorithm is required to be O(log (m+n)) .

 

2. Method 1: Merge Sort

Compare the last number of nums1 with the last number of nums2. If the value of nums1 is large, store the value in the new result array, and delete the last number of nums1, then the second-to-last number of nums1 becomes the last one. So just keep comparing the last number of nums1 and nums2.

class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums1.reverse()
        nums2.reverse()
        temp=[]
        while nums1 and nums2:
            if nums1[-1]<=nums2[-1]:
                temp.append(nums1[-1])
                nums1.pop()
            else:
                temp.append(nums2[-1])
                nums2.pop()
        if not nums1 and nums2:
            nums2.reverse()
            temp.extend(nums2)
        elif not nums2 and nums1:
            nums1.reverse()
            temp.extend(nums1)
        if len(temp)%2==1:
            return temp[len(temp)//2]
        else:
            return (temp[len(temp)//2]+temp[len(temp)//2-1])/2

Law 2: Divide and conquer law:

Points: Due to the median after merging the two arrays, compare the medians a and b of nums1 and nums2. If a<b, the median of the merged array is on the right side of a and the left side of b. If a>b, the median of the merged array is to the left of a and to the right of b.

  Suppose:

  nums1 median i = len (A) ///2

  nums2 median j = ki [k = (len (A) + len (B)) // 2]

Governance: There are three smallest subproblems:

  1. nums1 is empty, and nums2 has only one value [return the value of nums2, because the combined median is also the value]

  2. nums1 has only one value, nums2 is empty [return the value of nums1, because the combined median is also the value]

  3. Both nums1 and nums2 have only one value a and b [two values ​​a and b should be returned at this time. Continue to divide nums1 and nums2, and find that one case is empty and non-empty and can return a value, and the other case is a value that cannot be indexed to 1 in the array.

    Such as nums1=[1], nums2=[2]

    m=len(nums1)+len(nums2)=2, then the values ​​of k=m//2-1=0 and k=m//2=1 should be returned.

    When k=0, i=len(nums1)/2 =0, j=ki=0, [i,j as the median index of the two arrays] nums1[i]=1, nums[j]=2 , Compare the size of the two, 1<2, take the number on the right of 1 (including 1) as [1], and take the number on the left of 2 (excluding 2) as empty [], then [1] and [] return 1. ---------------- In this case only the smaller of nums1 and nums2 will be returned.

    When k=1, i=len(nums1)/2=0, j=ki=1, it is found that there is no such index, so the number cannot be returned when this happens. This is required to force it to return a value, which is the larger of nums1 and nums2 .

 

    So the third case: if k==1 and len(nums1)==1 and len(nums2)==1:

              return max(nums1[0],nums[0])

def findMid(nums1,nums2):
    l=len(nums1)+len(nums2)
    return findKthMid(nums1,nums2,l//2) if l%2==1 else (findKthMid(nums1,nums2,l//2-1)+findKthMid(nums1,nums2,l//2))/2

def findKthMid(nums1,nums2,k):
    if not nums1 and nums2:
        return nums2[k]
    if not nums2 and nums1:
        return nums1[k]
    if k==1 and len(nums1)==1 and len(nums2)==1:
        return max(nums1[0],nums2[0])
    i=len(nums1)//2
    j=k-i
    if nums1[i]>nums2[j]:
        return findKthMid(nums1[:i],nums2[j:],i)
    else:
        return findKthMid(nums1[i:],nums2[:j],j)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324687026&siteId=291194637