• Median_of_Two_Sorted_Arrays(理论支持和算法总结)


    可以将这个题目推广到更naive的情况,找两个排序数组中的第K个最大值(第K个最小值)。

    1、直接 merge 两个数组,然后求中位数(第K个最大值或者第K个最小值),能过,不过复杂度是 O(n + m)

    python

     1 class Solution(object):
     2     def findMedianSortedArrays(self, nums1, nums2):
     3         """
     4         :type nums1: List[int]
     5         :type nums2: List[int]
     6         :rtype: float
     7         """
     8         
     9         tmpresult = []
    10         n1 = 0
    11         n2 = 0
    12         
    13         while n1 < len(nums1) or n2 < len(nums2):
    14             if n1 == len(nums1):
    15                 tmpresult.append(nums2[n2])
    16                 n2 += 1
    17                 continue
    18             if n2 == len(nums2):
    19                 tmpresult.append(nums1[n1])
    20                 n1 += 1
    21                 continue
    22             if nums1[n1] < nums2[n2]:
    23                 tmpresult.append(nums1[n1])
    24                 n1 += 1
    25             else:
    26                 tmpresult.append(nums2[n2])
    27                 n2 += 1
    28                 
    29         if (n1+n2)&1 :
    30             return tmpresult[(n1+n2)/2]
    31         else:
    32             return (tmpresult[(n1+n2)/2 - 1] + tmpresult[(n1+n2)/2])/2.0

    2、不用直接merge两个数组,借助merge的思想,用两个指针pa和pb访问两个数组,size记录当前的找到了第几个数。时间复杂度O(k), 但是当k 很接近m+n时,这个方法还是O(m+n)。

    python

     1         n1 = 0
     2         n2 = 0
     3         left = -1
     4         right = -1
     5         midposition = 0
     6         leftnum = -1
     7         rightnum = -1
     8         if ((len(nums1) + len(nums2)) & 1):
     9             left = right = (len(nums1) + len(nums2))/2 
    10         else:
    11             left = (len(nums1) + len(nums2))/2 - 1
    12             right = (len(nums1) + len(nums2))/2 
    13         
    14         if len(nums1) == 0:
    15             return (nums2[left] + nums2[right])/2.0
    16         if len(nums2) == 0:
    17             return (nums1[left] + nums1[right])/2.0
    18             
    19         while n1 < len(nums1) or n2 < len(nums2):
    20             
    21             if n1 == len(nums1):
    22                 if midposition == left:
    23                     leftnum = nums2[n2]
    24                 if midposition == right:
    25                     rightnum = nums2[n2]
    26                 midposition += 1
    27                 if midposition > right:
    28                     break 
    29                 n2 += 1
    30                 continue
    31             
    32             if n2 == len(nums2):
    33                 if midposition == left:
    34                     leftnum = nums1[n1]
    35                 if midposition == right:
    36                     rightnum = nums1[n1]
    37                 midposition += 1
    38                 if midposition > right:
    39                     break 
    40                 n1 += 1
    41                 continue
    42             
    43             if nums1[n1] <= nums2[n2]:
    44                 
    45                 if midposition == left:
    46                     leftnum = nums1[n1]
    47                 if midposition == right:
    48                     rightnum = nums1[n1]
    49                 n1 += 1
    50                 midposition += 1
    51                 
    52             else:
    53                 
    54                 if midposition == left:
    55                     leftnum = nums2[n2]
    56                 if midposition == right:
    57                     rightnum = nums2[n2]
    58                 n2 += 1
    59                 midposition += 1
    60 
    61             if midposition > right:
    62                 break 
    63                 
    64         return (leftnum+rightnum)/2.0

    3、二分查找的思想

    我们可以考虑从k入手。如果我们每次都能够剔除一个一定在第k大元素之前的元素,那么我们需要进行k次。但是如果每次我们都剔除一半呢?所以用这种类似于二分的思想,我们可以这样考虑:

    Assume that the number of elements in A and B are both larger than k/2, and if we compare the k/2-th smallest element in A(i.e. A[k/2-1]) and the k-th smallest element in B(i.e. B[k/2 - 1]), there are three results:
    (Becasue k can be odd or even number, so we assume k is even number here for simplicy. The following is also true when k is an odd number.)
    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], that means all the elements from A[0] to A[k/2-1](i.e. the k/2 smallest elements in A) are in the range of k smallest elements in the union of A and B. Or, in the other word, A[k/2 - 1] can never be larger than the k-th smalleset element in the union of A and B.

    Why?(反证法证明)
    We can use a proof by contradiction. Since A[k/2 - 1] is larger than the k-th smallest element in the union of A and B, then we assume it is the (k+1)-th smallest one. Since it is smaller than B[k/2 - 1], then B[k/2 - 1] should be at least the (k+2)-th smallest one. So there are at most (k/2-1) elements smaller than A[k/2-1] in A, and at most (k/2 - 1) elements smaller than A[k/2-1] in B.So the total number is k/2+k/2-2, which, no matter when k is odd or even, is surly smaller than k(since A[k/2-1] is the (k+1)-th smallest element). So A[k/2-1] can never larger than the k-th smallest element in the union of A and B if A[k/2-1]
    Since there is such an important conclusion, we can safely drop the first k/2 element in A, which are definitaly smaller than k-th element in the union of A and B. This is also true for the A[k/2-1] > B[k/2-1] condition, which we should drop the elements in B.
    When A[k/2-1] = B[k/2-1], then we have found the k-th smallest element, that is the equal element, we can call it m. There are each (k/2-1) numbers smaller than m in A and B, so m must be the k-th smallest number. So we can call a function recursively, when A[k/2-1] < B[k/2-1], we drop the elements in A, else we drop the elements in B.


    We should also consider the edge case, that is, when should we stop?
    1. When A or B is empty, we return B[k-1]( or A[k-1]), respectively;
    2. When k is 1(when A and B are both not empty), we return the smaller one of A[0] and B[0]
    3. When A[k/2-1] = B[k/2-1], we should return one of them

    In the code, we check if m is larger than n to garentee that the we always know the smaller array, for coding simplicy.

     1 class Solution(object):
     2     def min(self,a,b):
     3         if a>b:
     4             return b
     5         else:
     6             return a
     7             
     8     def findKthsortedarray(self,nums1,nums2,k):
     9         if len(nums1) > len(nums2):
    10             tmp = nums1
    11             nums1 = nums2
    12             nums2 = tmp
    13         if len(nums1) == 0:
    14             return nums2[k-1]
    15         if k == 1:
    16             return min(nums1[0],nums2[0])
    17 
    18         p1 = min(k/2,len(nums1))
    19         p2 = k - p1
    20             
    21         if nums1[p1-1] == nums2[p2-1]:
    22             return nums1[p1-1]
    23         if nums1[p1-1] > nums2[p2-1]:
    24             return self.findKthsortedarray(nums1,nums2[p2:],k-p2)
    25         if nums1[p1-1] < nums2[p2-1]:
    26             return self.findKthsortedarray(nums1[p1:],nums2,k-p1)
    27                 
    28     def findMedianSortedArrays(self, nums1, nums2):
    29         num1 = len(nums1)
    30         num2 = len(nums2)
    31         
    32         if (num1+num2)&1:
    33             return self.findKthsortedarray(nums1,nums2,(num1+num2)/2+1)
    34         else:
    35             return (self.findKthsortedarray(nums1,nums2,(num1+num2)/2) + self.findKthsortedarray(nums1,nums2,(num1+num2)/2+1))/2.0
  • 相关阅读:
    JAVA理解逻辑程序的书上全部重要的习题
    体检套餐管理系统的综合版
    一路奔跑,一路寻找
    员工考勤信息管理小程序
    枚举的独特强大之处
    C#中HashTable的用法
    项目经理评分
    若想成功,请记住!
    数组的经典例子
    S1的小成果:MyKTV系统
  • 原文地址:https://www.cnblogs.com/strongYaYa/p/6418310.html
Copyright © 2020-2023  润新知