Minimum number of rotated arrays (sword refers to an offer interview question)

Source: Internet
Author: User

The idea of the knot in the sword refers to the analysis of the book is also more thorough, the analysis process here from the cattle network to the topic of the discussion is relatively good to a netizen analysis process

The rotated array can actually be divided into two ordered sub arrays: the front-face array is larger than the elements in the back-face array

Notice that the actual smallest element is the dividing line of the two sub arrays. The array given in this topic is somewhat sorted, so we try to find the smallest element by using the binary lookup method.

Ideas:

(1) We use two pointer left,right to point to the first element and the last element of the array, respectively. According to the rotation rules of the topic, the first element should be greater than the last element (there is no duplicate element).

But if not rotated, the first element must be less than the last element.

(2) Locate the middle element of the array.

The middle element is greater than the first element, the middle element is in the preceding incrementing array, at which point the smallest element is behind the middle element. We can make the first pointer left point to the middle element.

After the move, the first pointer is still in the previous incrementing array.

The middle element is less than the first element, and the middle element is in the subsequent incrementing array, at which point the smallest element is positioned before the middle element. We can have the second pointer right point to the middle element.

After the move, the second pointer is still in the subsequent incrementing array.

This will narrow the search.

(3) According to the above idea, the first pointer left always points to the element of the previous incrementing array, and the second pointer right always points to an array element that is incremented later.

Eventually the first pointer will point to the last element of the preceding array, and the second pointer to the first element in the following array.

That means they will point to two adjacent elements, and the second pointer is just the smallest element, which is the end condition of the loop.

So far the above thinking is very time-consuming to solve the situation without duplicate numbers, this topic added to this requirement, with a repeat number.

Therefore, this topic is more special than the previous topic:

Let's look at a set of examples: {1,0,1,1,1} and {1, 1, 1,0,1} can all be viewed as the rotation of the ascending sort array {0,1,1,1,1}.

In this case we can not continue to use the solution of a problem, to solve the problem. Because in both arrays, the first digit, the last number, the middle number is 1.

In the first case, the middle number is in the following sub array, and in the second case, the middle number is in the preceding sub array.

So when the two pointers point to the same number as the middle number, we cannot determine whether the median 1 belongs to the preceding sub array (green representation) or to the following sub Array (purple representation).

You cannot move the pointer to narrow the search.

New Ket Network test passed the following code:

Class Solution {
Public
int Minnumberinrotatearray (vector<int> rotatearray) {
if (rotatearray.size () = = 0)
return 0;
int index1=0;
int Index2=rotatearray.size ()-1;
int midindex=index1;
while (Rotatearray[index1]>=rotatearray[index2])
{
if (index2-index1==1)
{
Midindex=index2;
Break
}
midindex= (INDEX1+INDEX2)/2;
if (rotatearray[index1]==rotatearray[index2]&&
ROTATEARRAY[MIDINDEX]==ROTATEARRAY[INDEX1])
{
int RESULT=ROTATEARRAY[INDEX1];
for (int i=index1+1;i<rotatearray.size (); i++)
{
if (Rotatearray[i]<result)
Result=rotatearray[i];
}
return result;
}
if (Rotatearray[index1]<=rotatearray[midindex])
Index1=midindex;
else if (Rotatearray[midindex]<=rotatearray[index2])
Index2=midindex;
}
return Rotatearray[midindex];
}
};

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.