Ask the question of the big K

Source: Internet
Author: User

Problem: There are now two ordered arrays A and B, which are the elements of the K-large after merging the two arrays.

Method One, the use of two pointers, the merging of the sorting of two arrays in the way, there is no need to sort, only need to find the number of K after merging, so you need two pointers. Time complexity of $o (K) $

Method two, using binary search method will be the complexity of $o (log (K)) $

The overall idea of the algorithm is:

Assuming you want to find the K, we consider the first $\frac{k}{2}$ element in a and the first $\frac{k}{2}$ element in B. If A[K/2] is less than B[K/2] then the first k/2 elements of a are removed, whereas the first K/2 elements of B are removed. Then search for the small K/2 element in the following section. The next iteration follows the same idea. If you know the element you want to search directly, for example, if one of the array sizes is empty, search for the small k in the other array directly. If k is 1, the smallest person in a[0] b[0] is returned directly.

In fact, we think about why we have to delete the former K/2, so it must be right? We can cite an example.

A:1 3 6 9 11 12 35

B:2 4 14 15 17 28 30

A+b:1 2 3 4 6 9 11 12 14 15 17

K Big is 12, we send small throw away 13 6 9 is safe, why? All we have to do is make sure they're in the top K small. Because when they are in the first k small inside, after we lose them and then in the remaining elements to find the first K/2 small, is not affected. For example above

1 2 3 4 6 9 11 12, lose 1 3 6 9

to 2 4 11 12, the 4th small of the remaining elements is the original 8th. Why would that be? Simple anyway.

Assuming that the deleted element has a smaller element than the k, assuming X, there are at least k elements before X, but we know that the deleted array is less than x smaller than the k/2-1, and there is no k/2-2 in B, so the two add up, it is impossible to achieve k, so the original hypothesis is not established. Thereby they must be in the former K-small inside.

    Private intFindkth (int[]nums1,intStart1,intS1,int[] Nums2,intStart2,intS2,intk) {        if(S1&GT;S2)returnfindkth (nums2,start2,s2,nums1,start1,s1,k); if(s1==0)returnNums2[start2+k-1]; if(k==1)returnmath.min (Nums1[start1],nums2[start2]); intPa=math.min (S1,K/2); intpb=k-PA; if(nums1[start1+pa-1]<nums2[start2+pb-1]){            returnFindkth (nums1,start1+pa,s1-pa,nums2,start2,s2,k-PA); }        Else{            returnFindkth (nums1,start1,s1,nums2,start2+pb,s2-pb,k-pb); }    }

Ask the question of the big K

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.