Find the kth largest number in two sorted arrays

1. Problems

Given two sorted arrays, find the kth largest element among all elements of both

2. Solution 1: merge--turn two sorted arrays into one sorted array

Time complexity O(m+n), space complexity O(m+n)

/*************************************************
Given two sorted arrays, find the kth largest element among all elements of both
Author:tmw
date:2018-3-25
*************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int merge( int* array1, int len1, int* array2, int len2, int K )
{
    int i=0;
    int j=0;
    int k=0;
    if(!array1||!array2||len1==0||len2==0)
        return -1;
    int result[len1+len2];
    while( i<len1 && j<len2 )
    {
        if( array1[i] <= array2[j] )
            result[k++] = array1[i++];
        else
            result[k++] = array2[j++];
    }
    /**array1 has leftovers**/
    while( i<len1 )
        result[k++] = array1[i++];
    /**array2 has leftovers**/
    while( j<len2 )
        result[k++] = array2[j++];

    return result[K-1];
}

3. Solution 2: Cursor Counting

    The question only requires the k-th largest number. There is no need to reorder all the arrays. You can define two cursors to point to two sorted arrays, move them in order, and count them with count. When count is equal to k, return two cursors. The smallest of the numbers pointed to.

    Time complexity O(m+n), space complexity O(m+n)

int find_Kth_largeNumber_from2sortedArray( int* array1, int len1, int* array2, int len2, int K )
{
    if(!array1||!array2||len1==0||len2==0)
        return -1;
    int i = 0;
    int j = 0;
    int count = 0;

    while( count<K-1 )
    {
        if( array1[i]<=array2[j] )
            i++;
        else
            j++;
        count++;
    }
    return array1[i]>=array2[j]?array2[j]:array1[i];
}

4. Solution 3: Class Binary Search

Take advantage of the condition that both arrays are ordered:
    1) When array1[k/2-1] == array2[k/2-1], return array1[k/2-1] or array2[k/2- 1];
    2) When array1[k/2-1] > array2[k/2-1], the elements of array2 in the range of [0,k/2-1] must be smaller than the kth element after the combination of array1 and array2 Small, you can ignore the elements of array2 in the range of [0,k/2-1];

    3) When array1[k/2-1] < array2[k/2-1], the elements of array1 in the range of [0,k/2-1] must be smaller than the kth element after the combination of array1 and array2, which can be Don't consider the elements of array1 in the range [0,k/2-1].

    Therefore, the algorithm can be written in a recursive form. The condition for the end of the recursion is:
    1) array1[k/2-1] == array2[k/2-1] return array1[k/2-1]
    2) array1 or array2 is When empty, return array1[k-1] or array2[k-1]
    3) When k==1, return min(array1[0], array2[0])

    

    Time complexity O(log(m+n))

#define min(a,b) (a<b?a:b)
int Binary_find_Kth( int* array1, int len1, int* array2, int len2, int k )
{
    /**Always consider len1<len2**/
    if( len1>len2 ) return Binary_find_Kth(array2,len2,array1,len1,k);
    if( len1==0 ) return array2[k-1];
    if( k==1 ) return min(array1[0],array2[0]);

    /** Divide k into two parts and search on array1 and array2 arrays respectively**/
    int k1 = min (k / 2, len1);
    int k2 = k-k1;

    /**
        It means that the front part of k2-1 of array2 must be before the kth largest element, so:
        1) Skip this part of k2-1: update the first address index of the array, and update the length of the array at the same time;
        2) Include this k2 element into the range of the kth largest element found, and update the k value: k-k2
    **/
    if( array1[k1-1] > array2[k2-1] )
        return Binary_find_Kth(array1,len1,array2+k2,len2-k2,k-k2);
    /**
        It means that the front part of k1-1 of array1 must be before the kth largest element, so:
        1) Skip this part of k1-1: update the first address index of the array, and update the length of the array at the same time;
        2) Include this k1 element into the range of the kth largest element found, and update the k value: k-k1
    **/
    else if( array1[k1-1] < array2[k2-1] )
        return Binary_find_Kth(array1+k1,len1-k1,array2,len2,k-k1);

    else
        return array1[k1-1];
}


There is still a dream, if it comes true~~~ヾ(◍°∇°◍)ノ゙~~


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816134&siteId=291194637