The problem
Power button
Moving the beginning elements of an array to the end of the array is called array rotation. Take a rotation of an incrementally sorted array and print the smallest element of the rotation array. For example, the array [3,4,5,1,2] is a rotation of [1,2,3,4,5], whose minimum value is 1.
Example 1:
Input: [3,4,5, 2] Output: 1Copy the code
Example 2:
Input: [2,2,2,0,1] output: 0Copy the code
15. Attention: leetcode-cn.com/problems/fi…
Train of thought
Binary search
code
Python3
class Solution:
def minArray(self, numbers: List[int]) - >int:
left = 0
right = len(numbers) - 1
# Note the difference between this and labuladong template, the loop is left
while left < right:
mid = left + (right - left) // 2
Mid and right = mid and left = right
# The right side is ordered, and the minimum must be on the left
if numbers[mid] < numbers[right]:
right = mid
The minimum value must be on the right
elif numbers[mid] > numbers[right]:
left = mid + 1
Shrink the right border because it contains duplicates
[3,3,1,3] [3,3,1,3] [3,3,1,3
else:
right -= 1
return numbers[left]
Copy the code
link
GitHub