Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findMin(self, nums: List[int]) -> int:
- """
- Oh! We're looking for the peak in the list,
- where we're guaranteed there's only one peak.
- """
- def get_val(x):
- if x < 0:
- return -float('inf')
- if x >= len(nums):
- return float('inf')
- return nums[x]
- def is_peak(x):
- return get_val(x) < get_val(x-1) and get_val(x) < get_val(x+1)
- if len(nums) == 2:
- return min(nums)
- l, r = 0, len(nums) - 1
- midpoint = (r - l) // 2
- while not is_peak(midpoint) and l < r:
- print(l, midpoint, r)
- if nums[l] < nums[midpoint]:
- l = midpoint
- else:
- r = midpoint
- midpoint = l + (r - l) // 2
- print('after', l, midpoint, r)
- return min([nums[0], nums[midpoint], nums[-1]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement