Median of Two Sorted Arrays

来源:互联网 发布:生成app网站源码 编辑:程序博客网 时间:2024/05/21 22:49

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