Median of Two Sorted Arrays

本文介绍了一种高效算法来找到两个已排序数组的中位数。该算法将问题转化为寻找第k小的元素,利用分治策略实现,避免了直接合并数组带来的高时间复杂度。最终解决方案的时间复杂度达到了O(log(min(m, n))),其中m和n分别是两个数组的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
    int m = nums1.size();
    int n = nums2.size();
    vector<int> a;
    int i = 0;
    a.resize(m+n);
    copy(nums1.begin(),nums1.end(),a.begin());
    vector<int>::iterator it  = a.begin();
    while(i < m)
    {
        ++i;
        ++it;
    }
    copy(nums2.begin(),nums2.end(),it);
    sort(a.begin(),a.end());

    double median = double ( (m+n)%2 ? a[(n+m)>>1] : (a[(n+m)>>1]+a[(m+n-1)>>1])/2.0 );
    return median;
}

int main()
{
    int arr[] = {};
    int arr1[] = {2,3};
    vector<int> nums1;
    vector<int> nums2;
    for(int i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
        nums1.push_back(arr[i]);
    for(int i = 0; i < sizeof(arr1)/sizeof(arr1[0]); ++i)
        nums2.push_back(arr1[i]);
    cout<<findMedianSortedArrays(nums1,nums2)<<endl;
    return 0;
}

该方法居然也通过测试,但是其复杂度最坏情况为O(nlogn),这说明leetcode只对算法的正确性有要求,时间要求其实不严格。

改进方法:
该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]

double findKth(int a[], int m, int b[], int n, int k)  
{  
    //always assume that m is equal or smaller than n  
    if (m > n)  
        return findKth(b, n, a, m, k);  
    if (m == 0)  
        return b[k - 1];  
    if (k == 1)  
        return min(a[0], b[0]);  
    //divide k into two parts  
    int pa = min(k / 2, m), pb = k - pa;  
    if (a[pa - 1] < b[pb - 1])  
        return findKth(a + pa, m - pa, b, n, k - pa);  
    else if (a[pa - 1] > b[pb - 1])  
        return findKth(a, m, b + pb, n - pb, k - pb);  
    else  
        return a[pa - 1];  
}  

class Solution  
{  
public:  
    double findMedianSortedArrays(int A[], int m, int B[], int n)  
    {  
        int total = m + n;  
        if (total & 0x1)  
            return findKth(A, m, B, n, total / 2 + 1);  
        else  
            return (findKth(A, m, B, n, total / 2)  
                    + findKth(A, m, B, n, total / 2 + 1)) / 2;  
    }  
};  

我们可以看出,代码非常简洁,而且效率也很高。在最好情况下,每次都有k一半的元素被删除,所以算法复杂度为logk,由于求中位数时k为(m+n)/2,所以算法复杂度为log(m+n)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值