TOPK problem of monotone rotating array

Source: Internet
Author: User

Problem Description: Enter a monotone rotated array to find the element K small in the array.

Analysis: Many people see this topic will be a little confused, perhaps the reader does not know what is a rotating array, I first explain the next two concepts,

the definition of a rotating array : Moves the first few elements of an array to the end of the array, which is called the rotation of the array.

definition of a monotone rotation array : If the array is a monotone array before rotation, it is called a monotone rotation array after the rotation.

For the sake of explanation, here I explain the example of a monotone non-descending rotation array, other types can be used by analogy.

Solution One : The simplest way is to scan from the beginning in place, to find a small element subscript, and then calculate the K small element subscript, you can get the answer.

However, the time complexity of doing this is O (n). You can also find a faster solution.

Solution Two: make full use of the monotone nature of the array to solve the problem, you can find that the rotated array can be divided into two monotone sub-arrays, where all elements of a monotone subarray are greater than or equal to the elements in another array.

and the smallest element is exactly the decomposition point of two elements, so you can find it using a binary lookup method.

You can set three pointers left,right,mid to the subscript of the array head element, the tail element, and the middle element, respectively. Using the intermediate elements to compare with the head element, there are two situations:

If A[mid]>=a[left], make Left=mid

If A[mid]<a[left], make Right=mid

And then re-calculate the mid, so that has been circulating to do, until the Left+1==right stop, at this time rigth is the smallest element subscript, and then the K to calculate the sub-elements of the subscript can be.

The time complexity of this method is O (Logn).

Contrast: It is obvious that solution two is faster than solution one, but requires that the array must be a monotone array, and the solution one can be used for all rotating arrays. Because of the solution one, relatively simple, I am no longer programmed to implement, I mainly to the solution of two programming implementation.

For the general test sample, the solution is no problem, but for the special test sample, there is a problem.

For example: 1,0,1,1,1 and 1,1,1,0,1 can find the result is incorrect, so need to be improved, if the left,right,mid refers to the elements are equal, the solution of two helpless, need to use a solution.

We can deal with this particular condition in the procedure to judge alone.

In addition, solution two can be further optimized, if a[left]<a[right], we can directly find out a[left] is the smallest element, and then the K to find the small element K.

The specific Java code is as follows, the code is more generic, the reader can easily translate into other language implementations.

1  Public classmain{2      Public Static voidMINTOPK (intA[],intk) {3         if(a==NULL|| k<=0 | | K>a.length)//if the array does not exist or K does not meet the requirements, the direct end4{System.out.println ("does not exist");5               return ;6             }7         intP//Subscript used to denote the number of MINTOPK8         intLeft=0,right=a.length-1,mid= (Left+right)/2;//find the initial start, end, and intermediate element subscript9         if(A[mid]==a[left] && a[mid]==a[right])//If the three subscript refers to an element that is equal, the scan starts from the beginningTen              for(inti=1;i<a.length;i++) One                 if(a[i-1]>A[i]) A{p=i+k-1; -                     if(p>a.length-1) P=p-a.length;//to find out the subscript of MINTOPK - System.out.println (a[p]); the                     return; -                 } -         if(A[left]<a[right])//if the header element is smaller than the tail element, the left is the smallest element -         { +P=left+k-1; -             if(p>a.length-1) p=p-a.length; + System.out.println (a[p]); A             return; at         } -          -          while((left+1)!=right)//use three pointers to find -         {    -             if(A[mid]>=a[left]) left=mid; -             Elseright=mid; inMid= (left+right)/2; -         } toP=right+k-1; +         if(p>a.length-1) p=p-a.length; - System.out.println (a[p]); the     } *      Public Static voidMain (string[] args) { $         //TODO Auto-generated method stubsPanax Notoginseng         inta[]={3,4,5,1,2}; -         intK=5; the MINTOPK (a,k); +     } A  the}

The output is:

5

Note: I've only written a set of test examples in the code, and the reader can download the code to authenticate itself.

TOPK problem of monotone rotating array

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.