[C + +] leetcode:80 Find Minimum in rotated Sorted Array

Source: Internet
Author: User

Title:

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.

Assume no duplicate exists in the array.

Ideas:


Consider that we divide the sequence into two parts, one part will be ordered, and the other part contains the minimum value.

For example, a number [1,2,3,4,5,6,7] (Figure 1), which rotates from the position of three steps from the right-hand side, gets [5,6,7,1,2,3,4] (Figure 2), assuming that we rotate the sequence from the position of the K-step to the left, and get the subsequence [AL, Al+1, ..., AK], [AK +1, ..., AR].

Iteration criteria:

(1) If the ordered sequence is not rotated, that is, Al < AR, then Al is the minimum value, returning to Al;

(2) If the ordered sequence is rotated at least one step, then Al must be larger than AR;

Let's assume that M1 is a cutting point (MID) because Am1>ar, and in [AL ... AM1] Each element is greater than AR (because Al>ar, and the sequence is incremented), the minimum value will be in [am1+1 ... AR] Middle. (start = mid + 1;)

another case, if you choose M2 as the cutting Point (MID), Because AM2 <=ar, we know [am2+1 ... AR] Each element is larger than AM2 (increment), then the minimum value is [al ... AM2] in the middle. (end = mid;)

termination condition:

gives two examples: A = [+]; B = [2,1]

For A, 1 < 2 = AM < AR, and therefore it would set r = M = + R = 0.

For B, 2 > 1 = AM > AR, and therefore it'll set L = M + 1 = L = 1.

When L = = R, the iteration ends, we find the minimum value, so the outer condition of the loop (Start < end) cannot be equated.

Attention:

1. Iteration termination condition start = = end, the loop condition cannot take an equal sign

while (Start < end)
2. Judging from start to end is an ordered sequence, you can return to start directly.

/start to end is ordered sequence start is the minimum value; If you rotate only once, there will be no lower value between start and end            if (Num[start] < Num[end])            {                return Num[start] ;            }
3. Note that depending on the above analysis, determine the condition and interval of each iteration, and whether the mid is included should be noted.

Start to end does not satisfy orderly continue to find the smaller half minimum value in the right half            if (Num[start] <= Num[mid])            {                start = mid + 1;  Num[mid] >= Num[start] >= num[end], then mid must not be the minimum value can be skipped            }            else            {                end = mid;      Num[mid] < Num[start], mid has the lowest possible value and cannot be skipped.            }

Complexity: O (log (n))

AC Code:

Class Solution {public:    int findmin (vector<int> &num) {        int n = num.size ();        if (n = = 0) return 0;                int start = 0;        int end = n-1;                while (Start < end)        {            int mid = start + (End-start)/2;            Start to end is an ordered sequence start is the minimum value; the lower value between start and end does not occur            if (Num[start] < Num[end])            {                return num[ Start];            }                        Start to end does not satisfy orderly continue to find the smaller half minimum value in the right half            if (Num[start] <= Num[mid])            {                start = mid + 1;  Num[mid] >= Num[start] >= num[end], then mid must not be the minimum value can be skipped            }            else            {                end = mid;      Num[mid] < Num[start], mid has the lowest possible value and cannot be skipped.            }        }                return Num[start];}    ;


[C + +] leetcode:80 Find Minimum in rotated Sorted 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.