Space
Open
From Fieldwork
Scales
Archive
We move right and not mid because... we need to shrink the window. That's it, really. It's remarkably similar to the first one. Having duplicates is... a 2-line change, ish.
It's similar to:
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
Search in Rotated Sorted Array II
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] < nums[right]:
# Minimum is in [left..mid]
right = mid
elif nums[mid] > nums[right]:
# Minimum is in (mid..right]
left = mid + 1
else:
# Equal, so shrink the window
right -= 1
return nums[left]Practice bench
A private scratchpad for this reading. Nothing is sent or scored.
What is still unclear, or what would change the explanation?
Saved on this device · one draft per mode