The smallest number of rotated arrays (the sword refers to offer two points O (log n))

Source: Internet
Author: User

Minimum number of rotated array
    • Number of participants: 1866 time limit: 1 seconds space limit: 32768K
    • By scale: 15.04%
    • Best record: 0 ms|8552k(from left small right)
The title describes moving a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending sequence that outputs the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1.

Topic Link:http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?rp=1&ru=/ta/coding-interviews& Qru=/ta/coding-interviews/question-ranking


The original idea is violence, right! Is from the first enumeration to the last one, the time complexity of O (n); or the use of Monotonic enumeration to one of the less than the first to exit, the average time complexity is .... O ((1+n)/2) seems to be O (n) Ah, no bad;

Such a solution can not be recognized by the interviewer, nor reflect the original value of the problem, we should find a faster solution, two points, let the middle node to determine the next narrowing of the range (Eg:3 4 5 1 2, the next is 5 1 2, the next is 5 1; when Right-left==1 quits , the time complexity is O (log n), but some examples are not satisfied, eg 1 0 1 1 1 and 1 1 1 0 1) Yes, well, there's a case for this. When both values are equal to the median value, we should move the value of right forward, similar to violence, each error is an enumeration (Eg:1 1 1 1 1) Then you can only enumerate.


Do interview questions, not only to the topic AC, but also to think about time and space complexity, and better solution, must learn the boundary value of the judgment to prevent unnecessary errors.


#include <stdio.h> #include <iostream> #include <vector>using namespace Std;class solution{public://    Violence.        int MinNumberInRotateArray1 (vector<int> rotatearray) {int mmin=0x7fffffff;        for (int i=0; i<rotatearray.size (); i++) mmin=min (rotatearray[i],mmin);        Return mmin==0x7fffffff?0:mmin;//empty array returns 0}//monotonic int minNumberInRotateArray2 (vector<int> rotatearray) {        if (Rotatearray.size () ==0) return 0;        int mmin=rotatearray[0];            for (int i=1; i<rotatearray.size (); i++) {if (rotatearray[i]>=mmin) continue;        if (rotatearray[i]<mmin) {mmin=rotatearray[i];break;}    } return mmin;        }//two points int minNumberInRotateArray3 (vector<int> rotatearray) {if (Rotatearray.size () ==0) return 0;        int mmin=0x7fffffff;        int left=0;        int Right=rotatearray.size ()-1; if (left==right| | Rotatearray[left]<rotatearray[right]) return rotateArray[left];        int mm;            while (left<right) {mm= (left+right)/2; if (rotatearray[left]==rotatearray[right]&&rotatearray[right]==rotatearray[mm]) {right--            ;                } else if (rotatearray[left]<=rotatearray[mm]&&rotatearray[right]<rotatearray[mm]) {            left=mm;                } else if (rotatearray[left]>rotatearray[mm]&&rotatearray[right]>=rotatearray[mm]) {            right=mm;        } if (right-left==1) {mmin=min (rotatearray[left],rotatearray[right]);    } return mmin==0x7fffffff?0:mmin;//empty array returns 0}};int main () {solution so;    Vector<int> arr;    int n,a;    scanf ("%d", &n);        while (n--) {cin>>a;    Arr.push_back (a);    } int Ans1=so.minnumberinrotatearray1 (arr);    printf ("%d\n", ans1);    int Ans2=so.minnumberinrotatearray2 (arr);    printf ("%d\n", ans2); Int Ans3=so.minnumberinrotatearray3 (arr);    printf ("%d\n", ANS3); return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The smallest number of rotated arrays (the sword refers to offer two points O (log n))

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.