Find Minimum in rotated Sorted Array II

Source: Internet
Author: User

Catalogue:array-Division and Treatment method

Question

Suppose a sorted array is rotated on some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
The array may contain duplicates.

First try

Class Solution {public:    int findmin (vector<int> &num) {        ret = 0;        BinarySearch (Num,0,num.size ()-1);        return Num[ret];    }        void BinarySearch (vector<int> &num, int start, int end)    {        if (end-start<=1)        {            if (num[end ]>num[start]) return;            else            {                ret = end;                return;            }        }                int mid = (start + end) >> 1;        if (Num[start]>num[mid])          binarysearch (num, start, mid);        else if (Num[mid+1]>num[end])          binarysearch (num, mid+1, end);        else if (num[mid]>num[mid+1])          ret = mid+1;    } Private:    int ret;};
result:wrong!

Input: [10,1,10,10,10]
Output: 10
Expected: 1

Second try

There are repeating elements that are ignored by the while loop

Class Solution {public:    int findmin (vector<int> &num) {        ret = 0;        BinarySearch (Num,0,num.size ()-1);        return Num[ret];    }        void BinarySearch (vector<int> &num, int start, int end)    {        if (end-start<=1)        {            if (num[end ]>num[start]) return;            else            {                ret = end;                return;            }        }                int mid = (start + end) >> 1;        while (Start!=mid && num[start] = = Num[mid])        {            mid--;        }        while (End!=mid && num[end] = = Num[mid])        {            mid++;        }        if (Num[start] > Num[mid])          binarysearch (num, start, mid);        else if (num[mid+1] >= num[end])          binarysearch (num, mid+1, end);        else if (num[mid]>num[mid+1])          ret = mid+1;    } Private:    int ret;};
Result:runtime Error

Last executed input:[3,1,1]


Third try

Always be aware of data cross-border checks

Class Solution {public:    int findmin (vector<int> &num) {        ret = 0;        BinarySearch (Num,0,num.size ()-1);        return Num[ret];    }        void BinarySearch (vector<int> &num, int start, int end)    {        if (end-start<=1)        {            if (num[end ]>num[start]) return;            else            {                ret = end;                return;            }        }                int mid = (start + end) >> 1;        int mid2 = mid;        while (Start!=mid2 && num[start] = = Num[mid2])        {            mid2--;        }        if (Num[start] > Num[mid2])          binarysearch (num, start, MID2);        else        {            Mid2 = mid;            while (End!=mid && num[end] = = Num[mid])            {                mid++;            }            if (Num[mid] > Num[end])                BinarySearch (num, mid, end);        }    } Private:    int ret;};

result:wrong!

Input: [3,3,1,3]
Output: 3
Expected: 1


Fourth try:

Because there are duplicates, it is possible that mid and start or end are the same, but before start (or end) and mid have gone through the increment to decrement.

Class Solution {Public:int findmin (vector<int> &num) {ret = 0;        BinarySearch (Num,0,num.size ()-1);    return Num[ret];            } void BinarySearch (vector<int> &num, int start, int end) {if (end-start<=1) {            if (Num[end]>num[start]) return;                else {ret = end;            Return        }} int mid1 = (start + end) >> 1;        int mid2 = MID1;        while (start!=mid1 && num[mid1] = = Num[mid1-1]) {mid1--;        } while (Start!=mid1 && num[start] = = Num[start+1]) {start++; } if (Num[start] > Num[mid1] | |          (Start!=mid1 && Num[start] > Num[mid1-1]))        BinarySearch (num, start, MID1);            else {while (End!=mid2 && num[mid2] = = Num[mid2+1]) {mid2++; } while (End!=mid2 && nuM[end] = = Num[end-1]) {end--; } if (Num[mid2] > Num[end] | |                (End!=mid2 && Num[mid2] > num[end-1]))        BinarySearch (num, mid2, end); }}private:int ret;};
result:accepted



Find Minimum in rotated Sorted Array II

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.