Find Minimum in Rotated Sorted Array II
LeetCode 154 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Equal ⇒ high −= 1.
நான் Find Minimum in Rotated Sorted Array II (LeetCode 154) ஐ இப்படித் தீர்த்தேன் — Rotated Sorted Array பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Same comparison with the right end, plus one case for duplicates: when nums[mid] equals nums[high] you can't tell which side holds the minimum, but you can safely drop high because mid has the same value.
Approach
- 1
nums[mid] > nums[high] → minimum is right: low = mid + 1.
- 2
nums[mid] < nums[high] → high = mid.
- 3
Equal → high −= 1.
- 4
Return nums[low].
நான் எப்படித் தீர்த்தேன்
Binary search with duplicatesPythonO(log n) avg
class Solution:
def findMin(self, nums: List[int]) -> int:
low = 0
high= len(nums)-1
while low<high:
mid = (low+high)//2
#2,2,2,0,1 if nums[mid]>nums[high],smallest will be on the right half
if nums[mid]>nums[high]:
low = mid+1
#1,3,5 if nums[mid]<nums[high], the lowest must be on the left half, include mid 3,1,3 to handle this special case to include mid as well
elif nums[mid]<nums[high]:
high = mid
#10,1,10,10 reduce the search space if nums[mid] and nums[high] are equal
else:
high = high-1
return nums[low]Find Minimum in Rotated Sorted Array II — அடிக்கடி கேட்கப்படும் கேள்விகள்
Find Minimum in Rotated Sorted Array II பின்னாலுள்ள intuition என்ன?
Same comparison with the right end, plus one case for duplicates: when nums[mid] equals nums[high] you can't tell which side holds the minimum, but you can safely drop high because mid has the same value.
Find Minimum in Rotated Sorted Array II (LeetCode 154) ஐ எப்படித் தீர்ப்பது?
1. nums[mid] > nums[high] → minimum is right: low = mid + 1. 2. nums[mid] < nums[high] → high = mid. 3. Equal → high −= 1. 4. Return nums[low].
Find Minimum in Rotated Sorted Array II இன் time மற்றும் space complexity என்ன?
Time: O(log n) average, O(n) worst. Space: O(1). (Accepted approach: Binary search with duplicates.)
Find Minimum in Rotated Sorted Array II இல் பொதுவான தவறுகள் என்ன?
Don't jump high = mid on equality — the minimum could be between.