欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 一级特黄特黄xxx视频 | 天天干天天色 | 欧美专区在线 | 精品无人乱码一区二区三区 | 日韩欧美在线视频不卡免费视频 | 亚洲成人免费视频在线观看 | 国产欧美精品一区二区三区 | 亚洲精品久久久久久久久久久久久 | 日本簧片在线观看 | 91高清视频在线观看 | 亚洲a网 | 日本国产成人精品视频 | caoporn地址 | 欧美欧美欧美 | 免费视频爱爱太爽了 | 亚洲A片V一区二区三区有声 | 久久久www成人免费精品 | 五月天色婷婷在线 | 欧美亚洲理伦电影毛片在线播放 | 黑人巨大videosjapan高清 婷婷在线免费观看 | 亚洲在线视频观看 | h久久 | 精品一区二区久久久久久久网站 | 欧美精品 在线观看 | 四虎影视在线影院在线观看观看 | 哪里看毛片 | 91麻豆精品一二三区在线 | 日韩在线视频在线观看 | 男女啪啪高清无遮挡 | 伦理午夜电影免费观看 | 欧美一区欧美二区 | 日韩精选在线 | 欧美线人一区二区三区 | 精品日韩欧美国产一区二区 | 老人与老人免费a级毛片 | 欧美另类视频 | 国产精品视频免费播放 | 成人看片| 日韩欧美色图 | 免费精品美女久久久久久久久久 | 国产小视频在线播放 |