Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
조건
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
All the integers of nums are unique.
nums is sorted and rotated between 1 and n times.
class Solution:
def findMin(self, nums: List[int]) -> int:
l, r = 0, len(nums) - 1
while l < r:
m = (l+r)//2
if nums[r] > nums[m]:
r = m
else:
l = m + 1
return nums[l]
이진 탐색 사용
class Solution:
def findMin(self, nums: List[int]) -> int:
return min(nums)
결과적으로 둘다 정답을 출력한다,
속도도 별 차이 없다.