Median of Sorted Arrays

There are two sorted arrays A and B 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)).

double findMedian(int A[], int B[], int l, int r, int nA, int nB)
{
    if (l > r)
        return findMedian(B, A, max(0, (nA+nB)/2-nA), min(nB, (nA+nB)/2), nB, nA);
    int i = (l+r)/2;
    int j = (nA+nB)/2 - i - 1;
    if (j >= 0 && A[i] < B[j])
        return findMedian(A, B, i+1, r, nA, nB);
    else if (j < nB-1 && A[i] > B[j+1])
        return findMedian(A, B, l, i-1, nA, nB);
    else
    {
        if ((nA+nB)%2 == 1)
            return A[i];
        else if (i > 0)
            return (A[i]+max(B[j], A[i-1]))/2.0;
        else
            return (A[i]+B[j])/2.0;
    }
}

A O(m+n) solution:

bool findMedian(int A[], int B[], int nA, int nB)
{
    assert(A && B && nA >= 0 && nB >= 0);

    bool odd = (nA + nB) % 2 == 1 ? true : false;
    int medianIndex = (nA+nB)/2;
    if (odd == false)
        medianIndex++;

    int i = 0;
    int j = 0;
    int count = 0;
    int pre = -1;
    int cur = -1;
    while (i < nA && j < nB)
    {
        count++;
        if (A[i] < B[j])
        {
            if (count == medianIndex)
            {
                cur = A[i];
                break;
            }
            pre = A[i];
            i++;
        }
        else
        {
            if (count == medianIndex)
            {
                cur = B[i];
                break;
            }
            pre = B[j];
            j++;
        }
    }
    if (i == nA)
    {
        cur = B[j+medianIndex-count-1];
        if (medianIndex-count > 1)
            pre = B[j+medianIndex-count-2];
    }
    else if (j == nB)
    {
        cur = A[i+medianIndex-count-1];
        if (medianIndex-count > 1)
            pre = A[i+medianIndex-count-2];
    }

    if (odd == true)
        return cur;
    else
        return (cur+pre)/2.0;
}
时间: 2024-10-15 12:43:35

Median of Sorted Arrays的相关文章

[leetcode] 4. Median of Sorted Arrays

// Question: There are two sorted arrays A and B 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)).// Solution: get the new sorted array, then get the media number. #include

LeetCode OJ 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,

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,

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: 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

LeetCode题解-----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)). 解题思路: 本题要求求解的是两个有序序列的中位数.本质上就是求两个有序序列“第k小的数“的变形.假设两个有序序列一个长为m,另一个长为n,则我们

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)). Subscribe to see which companies asked this question 1 class Solution { 2 publ

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)). 题意: 两个排序后的数组nums1 和nums2,长度分别是m,n,找出其中位数,并且时间复杂度:O(log(m+n)) 最愚蠢的方法: 两个数组合

LeetCode #4 Median of Two Sorted Arrays (H)

[Problem] 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)). [Analysis] 由于两个数组已经是有序的,这题考的是如何将Linear复杂度优化到Log复杂度.那么很容易就能想到二分法或者说分治

LeetCode.004 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)). 题意 有两个排序好的数组nums1 & nums2.找到这两个数组的中位数. 思路 令sum等于两个数组大小的和. 如果sum==1,返回这个数. 否则,如