欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

LeetCode 4 - Median of Two Sorted Arrays

系統 1993 0

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

?

Thoughts:

Find K/2th index from first array, call it i and K/2th index from the second, call it j. ?Now consider this:
1.?If A[i] > B[j],?then?Kth smallest element?will include elements more than K/2 elements from array B and less than K/2 elements from array A. So our area of searching reduces to left part of K/2 index in array A and right part of k/2 index in array B, as shown in figure below.
Again since we have already found j definite smallest elements, we will reduce the search for (k-j)th smallest element in reduced search space.
Find Kth smallest in two sorted arrays
2. Similarly,?if A[i] < B[j],?our search space reduces to right part of k/2 index in array A and left of K/2 elements of array B, as shown in figure below. Again since we have already found i definite smallest elements, we will reduce the search for (k-i)th smallest element in reduced search space.
So what will be the base case:
When K is one, then we return the minimum of A[K-1] and B[K-1] (considering the zero based indexing of array) or if one of the array becomes null then return the other array's Kth smallest element.

Solution 1:

      public double findMedianSortedArrays(int A[], int B[]) {
    int m = A.length, n = B.length;
    if(((m+n)&1) == 1) {//m+n: odd
        return findKth(A, 0, m-1, B, 0, n-1, (m+n)/2);
    } else {
        return (findKth(A,0,m-1,B,0,n-1,(m+n)/2)+findKth(A,0,m-1,B,0,n-1,(m+n)/2-1))/2.0;
    }
}

public double findKth(int A[], int aStart, int aEnd, int B[], int bStart, int bEnd, int k) {
    int aLen = aEnd - aStart + 1;
    int bLen = bEnd - bStart + 1;
    if(aLen == 0) return B[bStart+k];
    if(bLen == 0) return A[aStart+k];
    if(k == 0) return Math.min(A[aStart], B[bStart]);
    
    int aMid = aLen*k/(aLen+bLen);
    int bMid = k-aMid-1;
    aMid += aStart;
    bMid += bStart;
    
    if(A[aMid] > B[bMid]) {
        k = k - (bMid - bStart + 1);
        aEnd = aMid;
        bStart = bMid + 1;
    } else {
        k = k - (aMid - aStart + 1);
        aStart = aMid + 1;
        bEnd = bMid;
    }
    return findKth(A, aStart, aEnd, B, bStart, bEnd, k);
}
    

Complexity of code to find median of two sorted array with above algorithm is O(log (m+n)).

?

?

Soltion 2:

?

      public double findMedianSortedArrays(int A[], int B[]) {
    int n = A.length;
    int m = B.length;
    // the following call is to make sure len(A) <= len(B).
    // yes, it calls itself, but at most once, shouldn't be
    // consider a recursive solution
    if (n > m)
        return findMedianSortedArrays(B, A);

    // now, do binary search
    int k = (n + m - 1) / 2;
    int l = 0, r = Math.min(k, n); // r is n, NOT n-1, this is important!!
    while (l < r) {
        int midA = (l + r) / 2;
        int midB = k - midA;
        if (A[midA] < B[midB])
            l = midA + 1;
        else
            r = midA;
    }

    // after binary search, we almost get the median because it must be between
    // these 4 numbers: A[l-1], A[l], B[k-l], and B[k-l+1] 

    // if (n+m) is odd, the median is the larger one between A[l-1] and B[k-l].
    // and there are some corner cases we need to take care of.
    int a = Math.max(l > 0 ? A[l - 1] : Integer.MIN_VALUE, k - l >= 0 ? B[k - l] : Integer.MIN_VALUE);
    if (((n + m) & 1) == 1)
        return (double) a;

    // if (n+m) is even, the median can be calculated by 
    //      median = (max(A[l-1], B[k-l]) + min(A[l], B[k-l+1]) / 2.0
    // also, there are some corner cases to take care of.
    int b = Math.min(l < n ? A[l] : Integer.MAX_VALUE, k - l + 1 < m ? B[k - l + 1] : Integer.MAX_VALUE);
    return (a + b) / 2.0;
}
    

?

?

Solution 3:

?

      int min(int a, int b) {
    return a > b ? b : a;
}

int find_kth(int a[], int b[], int size_a, int size_b, int k){
    /* to maintain uniformaty, we will assume that size_a is smaller than size_b
            else we will swap array in call :) */
    if(size_a > size_b) return find_kth(b, a, size_b, size_a, k);
    
    /* Now case when size of smaller array is 0 i.e there is no elemt in one array*/
    if(size_a == 0) return b[k-1]; // due to zero based index
    
    /* case where K ==1 that means we have hit limit */
    if(k == 1) return min(a[0], b[0]);

    /* Now the divide and conquer part */
    int i = min(size_a, k/2) ; // K should be less than the size of array  
    int j = min(size_b, k/2) ; // K should be less than the size of array  

    if(a[i-1] > b[j-1])
        // Now we need to find only K-j th element
        return find_kth(a, b+j, size_a, size_b-j, k-j);
    else
        return find_kth(a+i, b, size_a-i, size_b,  k-i);

    return -1;
}

double findMedianSortedArrays(int a[], int size_a, int b[], int size_b) {
    int left  =  (size_a + size_b + 1) >>1;
    int right =  (size_a + size_b + 2) >>1;
    return ( find_kth(a,b, size_a,size_b, left) + find_kth(a,b, size_a,size_b, right) )/2.0;
}
    

?

?

References:

http://www.algorithmsandme.com/2014/12/find-median-of-two-sorted-arrays-of.html

http://www.algorithmsandme.com/2014/12/fins-kth-smallest-element-in-two-sorted.html

https://oj.leetcode.com/discuss/11174/share-my-iterative-solution-with-o-log-min-n-m

LeetCode 4 - Median of Two Sorted Arrays


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 毛片在线不卡 | 亚洲播播播 | 日本三级带日本三级带黄国产 | 日本人69视频jizz免费看 | 亚洲成人精品 | 成人免费淫片aa视频免费 | 亚洲成人一区二区三区 | 亚洲天堂影院 | 91精品欧美久久久久久动漫 | 日韩中文一区二区三区 | 国产gav成人免费播放视频 | 99久久免费看精品 | 成人免费观看在线网址 | 日韩午夜精品 | 爱人同志国语免费观看全集 | 国产美女啪 | 中文字幕在线一区二区三区 | 国产精彩视频在线 | 色就是色欧美 | 九九热视频精品在线观看 | 国产高清在线91福利 | 亚洲第一国产 | 成人永久福利免费观看 | 蜜桃视频在线观看www社区 | 精品永久 | 在线欧美| 国产一级毛片高清视频完整版 | 麻豆av电影在线观看 | 99热这里只有精品8 免费看搡女人的视频 | 午夜电影合集 | 日韩一区二区免费看 | 亚洲午夜国产精品无卡 | 一级黄色淫片 | 极品美女aⅴ高清在线观看 一级片片 | 国产综合亚洲精品一区二 | 欧美日韩久久久 | 亚洲99| 在线播放日本爽快片 | 99精品免费视频 | 日本久久中文字幕 | 91在线观看视频 |