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

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
难度:Hard


思路

参考:LeetCode Discussion

https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/
一看到中位数就想着排序之类的事情,直接找到中间那个数或者两个数,但是后来就发现这种思路是不靠谱的,转变一下思路一下子就豁然开朗。找中位数实际上是将一堆数分成两堆,然后小的那堆最大和大的那堆最小之平均就是中位数。
那么我们所知的量就有:两个数组的大小,分割后两堆数的个数。
然后还有一个重要的关系,我们可以从第一个数组被分到小的那堆的个数推出第二个数组被分到小的那堆的个数,因为两堆数的个数是基本相同的(可能由于总数奇偶的关系,不能完全对半分)
那么如何判断两堆数是被正确拆分了呢?
如表:

A[0] A[i-1] A[i] A[m-1]
B[0] B[j-1] B[j] B[n-1]

左边是小的那堆,右边是大的那堆,这两堆数应该满足的关系为MAX(A[i], B[j]) < MIN(A[i+1], B[j+1]), 由于他们是有序的,所以满足的关系为A[i]

答案


// @author 吴博文
#include 
#include 
class Solution {
public:
    double getMedian(vector<int> & a) {
        if (a.size() % 2) {
            return a[a.size() / 2];
        } else {
            return double(a[a.size() / 2] + a[a.size() / 2 - 1]) / 2;
        }
    }
    double findMedianSortedArrays(vector<int>& A, vector<int>& B) {
        if (A.size() == 0) {
            return getMedian(B);
        } else if (B.size() == 0) {
            return getMedian(A);
        }
        int m = A.size();
        int n = B.size();
        if(m > n) {
            std::swap(A, B);
            std::swap(m, n);
        }
        int i, j;
        int down = 0;
        int up = m;
        int halfLength = (m + n) / 2;
        // binary search
        while (down <= up) {
            i = (down + up) / 2;
            j = halfLength - i;
            std::cout << i << " " << j << std::endl;
            if (0 == j) {
                if (B[j] >= A[i - 1]) {
                    break;
                } else {
                    up = i - 1;
                    continue;
                }
            } else if (0 == i) {
                if (A[i] >= B[j - 1]) {
                    break;
                } else {
                    down = i + 1;
                    continue;
                }
            }
            // end condition
            if (B[j] >= A[i - 1] && A[i] >= B[j - 1]) {
                break;
            } else if (A[i] < B[j - 1]) {
                down = i + 1;
            } else if (B[j] < A[i - 1]) {
                up = i - 1;
            }
        }
       int maxOfLeft, minOfRight;
       if (0 == i) {
           maxOfLeft = B[j - 1];
       } else if (0 == j) {
           maxOfLeft = A[i - 1];
       } else {
           maxOfLeft = std::max(A[i - 1], B[j - 1]);
       }

       if (m == i) {
           minOfRight = B[j];
       } else if (n == j) {
           minOfRight = A[i];
       } else {
           minOfRight = std::min(A[i], B[j]);
       }

       if ((m + n) % 2) {
           return minOfRight;
       } else {
           return (double)(maxOfLeft + minOfRight) / 2;
       }
    }
};

你可能感兴趣的:(leetcode)