Rotate the smallest number in the array
Source: leetcode https://leetcodecn.com/problems/xuanzhuanshuzudezuixiaoshuzilcof
subject
Moving the first elements of an array to the end of an array is called array rotation. Input a rotation of an incremental sorted array and output 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.
Example 1:
Input: [3,4,5,1,2]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note: this question is the same as 154 questions in the main station https://leetcodecn.com/probl…
Thinking of problem solving
Idea: binary search
First examine the question and repeat the concept of rotation array. Here, rotating an array means placing the first elements of the array in ascending order at the end of the array.
Well, according to this concept, we can find out. After the array is rotated, the original array will be changednums
Split into two ascending sorted arrays, set tonums1
、nums2
And is placed at the end of the arraynums2
Will be less than or equal to the preceding part of the arraynums1
This is because the element may be repeated and will be analyzed later. Now the question is how to find the two ascending arrays after rotation? Because just find the two rotated arrays,nums2
The first element in the array is the required answer.
Well, here, we can consider using the binary search method to find a dividing point (the dividing point is the first element placed at the end of the ascending array), and the array to the left of the demarcation point isnums1
, the array to the right of the dividing point (including the dividing point) isnums2
。
The specific ideas are as followsnums1
Represents the left ascending array after rotation,nums2
Represents the right ascending array at the end of the rotation:
 First, define the pointer
left,right
Respectively point tonums
Array, andmid
Is the midpoint of each bisection search; 
Start the comparison（
nums1
The element of is greater than or equal tonums2
Elements of）There are three situations: If
nums[mid] > nums[right]
When, thenmid
By all meansnums1
In this array, the dividing point is in the interval(mid, right]
, orderleft=mid+1
；  If
nums[mid] < nums[right]
When, thenmid
Then in thenums2
In the array, the dividing point falls in the interval[left, mid]
, orderright=mid
;  If
nums[mid] == nums[right]
(this is because the elements are allowed to repeat), there will be a fuzzy boundary. First of all, let’s talk about the conclusion (later analysis), let’sright
The pointer moves forward, causingright = 1
。
 If
Here it isnums[mid] == nums[right]
Suppose the given array is[1, 1, 0, 1]
and[1, 0, 1, 1, 1]
Then there will be the following situation

When given an array
[1, 1, 0, 1]
In this case,left = 0, right = 3, mid = 1
：
nums[mid] == nums[right]
At this time, mid is in the array on the leftnums1
Medium.


When given an array
[1, 0, 1, 1, 1]
At this timeleft = 0, right = 4, mid = 2
：
nums[mid] == nums[right]
, but the mid is in the array on the rightnums2
Medium.

In this case, it is impossible to directly judge whether the mid falls in thenums1
stillnums2
Medium. In the previous conclusion, letright = 1
In order to solve this problem, according to the above analysis, the feasibility of this conclusion is analyzed

When
mid
Fall onnums1
Array becausenums1
Any element of is greater than or equal tonums2
In other words, let the boundary point bepoint
）nums[point] <= nums[mid] == nums[right]
Then: if
nums[point] < nums[right]
It is explained hereright
The element on the left also has a small valueright = 1
After that,point
Is it still there[left, right]
In this interval;  If
nums[point] == nums[right]
We should pay attention to the dividing point indexpoint
Andright
In the case of equality. At this time, makeright=1
At this timepoint
Will not be in[left, right]
In this interval. Suppose you have the following array[1, 1, 2, 1]
Here it isleft = 0, right = 3, mid = 1
We can see that the dividing point is the last one. herepoint == right
, ifright = 1
So the last one here1
Will be discarded. But the end result will come back1
Because we give up the last1
After that, continue the binary search, and the cutoff point must fall on the[left, mid]
And the values of the elements in this interval are equal tonums[point]
。 So it can return the correct value.
 if
 When
mid
Fall onnums2
Array, that is to say[mid, right]
The values of all elements in this interval are equal. At this timeright = 1
It just discards a duplicate value, andpoint
Or did it fall in[left, right]
In this interval.
The following is an illustration of the algorithm implementation:
The specific implementation code is as follows.
code implementation
class Solution:
def minArray(self, numbers: List[int]) > int:
left = 0
right = len(numbers)  1
while left < right:
#Take the midpoint
mid = left + (right  left) // 2
#If numbers [mid] > numbers [right], the dividing point falls on (mid, right]
if numbers[mid] > numbers[right]:
left = mid + 1
#Numbers [mid] < numbers [right], the dividing point falls in [left, mid]
elif numbers[mid] < numbers[right]:
right = mid
#Numbers [mid] = = numbers [right], let right  = 1. The feasibility of this conclusion has been analyzed in this paper
else:
right = 1
return numbers[left]
Achieving results
Welcome to our attention
The official account [book collection]