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

解题思路:现在考虑一个更通用的问题:给定两个已经排序好的数组,找到两者所有元素中第k大的元素。假设两个数组A和B的元素个数都大于k/2,我们将A的第k/2个元素A[k/2-1]和B的第k/2个元素B[k/2-1]进行比较,有以下三种情况:

1. A[k/2-1] == B[k/2-1]

2. A[k/2-1] > B[k/2-1]

3. A[k/2-1] < B[k/2-1]

如果是A[k/2-1] < B[k/2-1]则说明A[0]到A[k/2-1]肯定在AUB的top k元素的范围内,反之则B[0]到B[k/2-1]肯定在AUB的top k元素的范围内。如果A[k/2-1] == B[k/2-1],说明第k大的元素为A[k/2-1]或B[k/2-1](这里假设排序好的数组是从小到大排列的,并且假设k是偶数。所的结论对k是奇数也是成立的)。

参考代码:

class Solution
{
 public:
  double findMedianSortedArrays(const vector<int>& A, const vector<int>& B) {
      const int m = A.size();
      const int n = B.size();
      int total = m + n;
      if (total & 1)
          return find_kth(A.begin(), m, B.begin(), n, total / 2 + 1);
      else
          return (find_kth(A.begin(), m, B.begin(), n, total / 2)
                  + find_kth(A.begin(), m, B.begin(), n, total / 2 + 1)) / 2.0;
  }

  int find_kth(std::vector<int>::const_iterator A, int m,
               std::vector<int>::const_iterator B, int n, int k)
  {
      //always assume that m is equal or smaller than n
      if (m > n) return find_kth(B, n, A, m, k);
      if (m == 0) return *(B + k - 1);
      if (k == 1) return min(*A, *B);

      //divide k into two parts
      int ia = min(k / 2, m), ib = k - ia;
      if (*(A + ia - 1) < *(B + ib - 1))
          return find_kth(A + ia, m - ia, B, n, k - ia);
      else if (*(A + ia - 1) > *(B + ib - 1))
          return find_kth(A, m, B + ib, n - ib, k - ib);
      else
          return A[ia - 1];
  }
};

运行结果:

median: 2.5
原文地址:https://www.cnblogs.com/pursuiting/p/10479923.html