LeetCode 4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 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)).

You may assume nums1 and nums2 cannot be both empty.

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


最简单的第一想法是,将两个数组merge,然后直接根据下表取到中位数,但由于本题的时间复杂度要求log(m+n),而merge做法的时间复杂度根据计算为O(m+n),不符题目要求,需要寻求优化的算法。

以下为算法思路。
由于算法要求为O(log(m+n)),考虑分治算法。

试考虑两个长度分别为n1,n2的排序数组A1,A2在这种情况下,若merge之后的数组的中位数下标k在merge数组中为(n1+n2)/2(暂不考虑奇偶问题),那存在整数i,j使得(i+j)-2 == k
现在问题转化为如何分别在A1,A2中寻找i,j使得上式成立。
分别考虑A1,A2的中位数m1,m2,这将merge数组分成了四块。
当m1==m2,就意味着m1或m2刚好为中位数,
而当m1大于m2就意味着,中位数出现在merge数组的后三块中,可以递归的在后面搜索。
m1小于m2以此类推。
如此这般就可以分治的解决找到排序数组中第(n1+n2)/2个数的问题。


代码如下

    def median(A, B):
        m, n = len(A), len(B)
        if m > n:
            A, B, m, n = B, A, n, m
        if n == 0:
            raise ValueError

        imin, imax, half_len = 0, m, (m + n + 1) / 2
        while imin <= imax:
            i = (imin + imax) / 2
            j = half_len - i
            if i < m and B[j-1] > A[i]:
                # i is too small, must increase it
                imin = i + 1
            elif i > 0 and A[i-1] > B[j]:
                # i is too big, must decrease it
                imax = i - 1
            else:
                # i is perfect

                if i == 0: max_of_left = B[j-1]
                elif j == 0: max_of_left = A[i-1]
                else: max_of_left = max(A[i-1], B[j-1])

                if (m + n) % 2 == 1:
                    return max_of_left

                if i == m: min_of_right = B[j]
                elif j == n: min_of_right = A[i]
                else: min_of_right = min(A[i], B[j])

                return (max_of_left + min_of_right) / 2.0
                turn (max_of_left + min_of_right) / 2.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值