Time：2020-12-19

# Rotate the smallest number in the array

Source: leetcode https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof

## 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://leetcode-cn.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 changed`nums`Split into two ascending sorted arrays, set to`nums1``nums2`And is placed at the end of the array`nums2`Will be less than or equal to the preceding part of the array`nums1`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 is`nums1`, the array to the right of the dividing point (including the dividing point) is`nums2`

The specific ideas are as follows`nums1`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 to`nums`Array, and`mid`Is the midpoint of each bisection search;
• Start the comparison（`nums1`The element of is greater than or equal to`nums2`Elements of）There are three situations:

• If`nums[mid] > nums[right]`When, then`mid`By all means`nums1`In this array, the dividing point is in the interval`(mid, right]`, order`left=mid+1`
• If`nums[mid] < nums[right]`When, then`mid`Then in the`nums2`In the array, the dividing point falls in the interval`[left, mid]`, order`right=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’s`right`The pointer moves forward, causing`right -= 1`

Here it is`nums[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 left`nums1`Medium.
• When given an array`[1, 0, 1, 1, 1]`At this time`left = 0, right = 4, mid = 2`

• `nums[mid] == nums[right]`, but the mid is in the array on the right`nums2`Medium.

In this case, it is impossible to directly judge whether the mid falls in the`nums1`still`nums2`Medium. In the previous conclusion, let`right -= 1`In order to solve this problem, according to the above analysis, the feasibility of this conclusion is analyzed

• When`mid`Fall on`nums1`Array because`nums1`Any element of is greater than or equal to`nums2`In other words, let the boundary point be`point``nums[point] <= nums[mid] == nums[right]`Then:

• if`nums[point] < nums[right]`It is explained here`right`The element on the left also has a small value`right -= 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 index`point`And`right`In the case of equality. At this time, make`right-=1`At this time`point`Will not be in`[left, right]`In this interval. Suppose you have the following array`[1, 1, 2, 1]`Here it is`left = 0, right = 3, mid = 1`We can see that the dividing point is the last one. here`point == right`, if`right -= 1`So the last one here`1`Will be discarded. But the end result will come back`1`Because we give up the last`1`After that, continue the binary search, and the cut-off point must fall on the`[left, mid]`And the values of the elements in this interval are equal to`nums[point]`。 So it can return the correct value.
• When`mid`Fall on`nums2`Array, that is to say`[mid, right]`The values of all elements in this interval are equal. At this time`right -= 1`It just discards a duplicate value, and`point`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]

## [Q & A share the second bullet] MySQL search engine, after watching the tyrannical interviewer!

Hello, I’m younger brother. A few days ago, I shared the second interview question, the interview site of search engine in MySQL. This question is the interview at normal temperature. After reading it, I’m sure you will gain something in terms of database engine If you haven’t read my first share, you can refer to […]