Find Minimum in Rotated Sorted Array LeetCode solution in Python

1
class Solution:
2
def findMin(self, nums: List[int]) -> int:
3
if len(nums) == 1 or nums[0] < nums[-1]:
4
return nums[0]
5

6
l, r = 0, len(nums) - 1
7

8
while l <= r:
9
mid = l + (r - l) // 2
10

11
if mid > 0 and nums[mid - 1] > nums[mid]:
12
return nums[mid]
13

14
# We compare the middle number and the right index number
15
# but why we cannot compare it with the left index number?
16
if nums[mid] > nums[r]:
17
l = mid + 1
18
else:
19
r = mid - 1

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0