[Nowcoder] Two unequal-length arrays for K-large number

Source: Internet
Author: User

Given two ordered arrays of arr1 and arr2, given an integer k, returns the small number of k in all the numbers of the two arrays.
For example:
arr1 = {1,2,3,4,5};
ARR2 = {3,4,5};
K = 1;
Since 1 is the smallest of all the numbers, it returns 1;

ARR1 = {n/a};
ARR2 = {3,4,5,6};
K = 4;
Since 3 is the 4th small number in all numbers, it returns 3;

Requirements: If the length of the arr1 is N,ARR2, the time complexity should be O (log (Min{m,n})).

The difficulty of this topic is the time complexity of the O (log (Min{m,n})), the reference http://www.cnblogs.com/diegodu/p/3790860.html can reach O (log (m+n))

The method of using http://www.cnblogs.com/diegodu/p/4589847.html to find the median of the equal-length array can be realized.

Example

ARR1 1 ~ 10,

ARR2 1 ' ~ 20 '.

When kth = 7 o'clock, kth < min (s1,s2),

The median of the first k elements of the two array is obtained, because the nth element must be in the first k elements of the two arrays.

Getupmedian (arr1, 0, kth-1, arr2, 0, kth-1);

When kth = 26 o'clock, Kth>max (S1,S2)

ARR1 1 to 5 is impossible, even if 5>20, but the 25th, not 26.

ARR2 1 ' to 15 ' is impossible, even if the >10, but the 25th, not 26.

So ruled out the 5+15 elements, arr1 left 5, arr2 left 5.

If we ask for the upper median of the remaining 10 elements, we can get the 25th largest, not the 26th,

So, we can examine the ARR1 6 and arr2 16 ' is not the answer alone,

If the direct return, not the words and ruled out two, excluding 22, the remaining 8, the median number fourth, plus the 26th one.

When kth = 16 o'clock, min (s1, S2) <kth<max (S1,S2)

ARR1 can not be ruled out, it is possible.

ARR2 's 1 ' to ' exclude, even if 5 ' is greater than ARR1 's 10, is only 15.

ARR2 17 ' To the exclusion, 17 ' would have ranked 17, the smallest is to shoot 17.

So arr2 has 6 ' ~ 16 ' 11 elements left, and arr1 has 10 elements,

In order to use the upper-median algorithm, you can individually detect 6 ', if returned, if not eliminated, the remaining use of the median algorithm.

http://www.nowcoder.com/profile/864393/test/231563/24588

classSolution { Public:        intGetupmedian (vector<int> arr1, vector<int>arr2) {         if(Arr1.size ()! =arr2.size ())return-1; if(arr1.size () = =0)            return-1; returnGetupmedian (ARR1,0, Arr1.size ()-1, ARR2,0, Arr1.size ()-1 ); }         intGetupmedian (Constvector<int> & arr1,intStart1,intEnd1,Constvector<int> & ARR2,intStart2,intEnd2) {            //cout << "start1\t" << start1 << Endl; //cout << "end1\t" << end1 << Endl; //cout << "start2\t" << start2 << Endl; //cout << "end2\t" << end2 << Endl;             if(Start1 = =end1) {                returnmin (Arr1[start1], arr2[start2]); }             intSize = End1-start1 +1; inthalfsize; if(Size &0x1==0x1) {halfsize= (size +1)/2; }            Else{halfsize= size/2; }             if(Arr1[start1 + halfsize-1] = = Arr2[start2 + halfsize-1])                returnArr1[start1 + halfsize-1]; Else if(Arr1[start1 + halfsize-1] > Arr2[start2 + halfsize-1])                returnGetupmedian (arr1, Start1, Start1 + halfsize-1, ARR2, End2-(halfsize-1), End2); Else //if (Arr1[start1 + halfSize-1] > arr2[start2 + halfSize-1])                returnGetupmedian (arr1, end1-(halfsize-1), End1, arr2, Start2, Start2+ Halfsize-1); }         intFindkthnum (vector<int> arr1, vector<int> arr2,intkth) {            intSize1 =arr1.size (); intSize2 =arr2.size (); if(Size1 >size2)returnFindkthnum (ARR2, arr1, KTH);//ensure size1 < size2             if(KTH <=size1)returnGetupmedian (ARR1,0, KTH-1, ARR2,0, KTH-1); Else if(kth <= size2 && kth >size1)//i.e.s1 = ten, S2 = kth = 14; //for ARR2, KTH + 1 ~ size2 (~-) is impossibly answer//for ARR2, 1 to Kth-size1-1 (1 ~ 3) is impossibly answer//so arr2 have 20-6-3 = numbers, but arr1 have numbers//so Jundge one element of arr2 Firstly, the call Getupmedian           {                if(Arr2[kth-size1-1] >= Arr1[size1-1] )                    returnArr2[kth-size1-1]; returnGetupmedian (ARR1,0, size1-1, ARR2, Kth-size1, kth-1); }            Else //if (kth > Size2)//i.e.s1 = ten, S2 = KTH = 25; //First 4 (kth-s2-1) of arr1 is impossibly answer. So just remove them//First (kth-s1-1) of arr2 is impossibly answer.so just remvoe them//ARR1 still has S1-(kth-s2-1) = S1 + s2-kth + 1 numbers (6)//ARR2 still has S2-(kth-s1-1) = S1 + s2-kth + 1 numbers (6)//we removes 2 * kth-s1-s2-2 numbers, (+)//Now we junge if arr1[kth-s2-1] and arr2[kth-s1-1] are the answer,if not, we remother them//Then we removes 2 * kth-s1-s2 numbers ()//and arr1 and arr2 both have S1 + s2-kth numbers (5)//We just calc Upmedia of them and got the answer, 5 =           {                if(arr2[kth-size1-1] >= arr1[size1-1])                    returnArr2[kth-size1-1]; if(arr1[kth-size2-1] >= arr2[size2-1])                    returnarr1[kth-size2-1]; returnGetupmedian (arr1, Kth-size2, size1-1, ARR2, Kth-size1, size2-1); }           }};

[Nowcoder] Two unequal-length arrays for K-large number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.