[LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

简介:

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.

Example

Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5.

Given A=[1,2,3] and B=[4,5], the median is 3.

Challenge

The overall run time complexity should be O(log (m+n)).

LeetCode上的原题,请参见我之前的博客Median of Two Sorted Arrays

解法一:

class Solution {
public:
    /**
     * @param A: An integer array.
     * @param B: An integer array.
     * @return: a double whose format is *.5 or *.0
     */
    double findMedianSortedArrays(vector<int> A, vector<int> B) {
        int n1 = A.size(), n2 = B.size();
        if (n1 < n2) return findMedianSortedArrays(B, A);
        if (n2 == 0) return ((double)A[(n1 - 1) / 2] + (double)A[n1 / 2]) / 2;
        int left = 0, right = n2 * 2;
        while (left <= right) {
            int mid2 = (left + right) / 2;
            int mid1 = n1 + n2 - mid2;
            double L1 = mid1 == 0 ? INT_MIN : A[(mid1 - 1) / 2];
            double L2 = mid2 == 0 ? INT_MIN : B[(mid2 - 1) / 2];
            double R1 = mid1 == n1 * 2 ? INT_MAX : A[mid1 / 2];
            double R2 = mid2 == n2 * 2 ? INT_MAX : B[mid2 / 2];
            if (L1 > R2) left = mid2 + 1;
            else if (L2 > R1) right = mid2 - 1;
            else return (max(L1, L2) + min(R1, R2)) / 2;
        }
        return -1;
    }
};

解法二:

class Solution {
public:
    /**
     * @param A: An integer array.
     * @param B: An integer array.
     * @return: a double whose format is *.5 or *.0
     */
    double findMedianSortedArrays(vector<int> A, vector<int> B) {
        int m = A.size(), n = B.size(), left = (m + n + 1) / 2, right = (m + n + 2) / 2;
        return (findKth(A, B, left) + findKth(A, B, right)) / 2.0;
    }
    double findKth(vector<int> A, vector<int> B, int k) {
        int m = A.size(), n = B.size();
        if (m > n) return findKth(B, A, k);
        if (m == 0) return B[k - 1];
        if (k == 1) return min(A[0], B[0]);
        int i = min(m, k / 2), j = min(n, k / 2);
        if (A[i - 1] > B[j - 1]) {
            return findKth(A, vector<int>(B.begin() + j, B.end()), k - j);
        } else {
            return findKth(vector<int>(A.begin() + i, A.end()), B, k - i);
        }
        return -1;
    }
};

本文转自博客园Grandyang的博客,原文链接:两个有序数组的中位数[LintCode] Median of Two Sorted Arrays ,如需转载请自行联系原博主。

相关文章
|
10月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
30 0
|
Python
LeetCode 349. Intersection of Two Arrays
给定两个数组,编写一个函数来计算它们的交集。
63 0
LeetCode 349. Intersection of Two Arrays
LeetCode 350. Intersection of Two Arrays II
给定两个数组,编写一个函数来计算它们的交集。
67 0
LeetCode 350. Intersection of Two Arrays II
Leetcode-Hard 4. Median of Two Sorted Arrays
Leetcode-Hard 4. Median of Two Sorted Arrays
93 0
|
人工智能
LeetCode之Intersection of Two Arrays
LeetCode之Intersection of Two Arrays
93 0
LeetCode 350: 两个数组的交集 II Intersection of Two Arrays II
题目: 给定两个数组,编写一个函数来计算它们的交集。 Given two arrays, write a function to compute their intersection. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [4,9] 说明: 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
713 0