Find Minimum in Rotated Sorted Array
LeetCode 153 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
nums[mid] < nums[high] ⇒ high = mid, else low = mid + 1.
मैंने Find Minimum in Rotated Sorted Array (LeetCode 153) ऐसे हल किया — Rotated Sorted Array पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।
नीचे का विवरण अंग्रेज़ी में है।
Intuition
Compare mid with the right end. If nums[mid] < nums[high], the right half is sorted and the minimum is at mid or to its left; otherwise the drop — and the minimum — is to the right of mid.
Approach
- 1
low < high loop.
- 2
Right half sorted → high = mid (mid may be the min).
- 3
Otherwise low = mid + 1.
- 4
Return nums[low].
मैंने कैसे हल किया
Binary search on the dropPythonO(log n) time
class Solution:
def findMin(self, nums: List[int]) -> int:
#find mini in which half
low = 0
high = len(nums)-1
while low<high:
mid = (low+high)//2
if nums[mid]<nums[high]:#remove the right half
high = mid
else:
low = mid+1 #remove the left half
return nums[low]
Find Minimum in Rotated Sorted Array — अक्सर पूछे जाने वाले प्रश्न
Find Minimum in Rotated Sorted Array के पीछे intuition क्या है?
Compare mid with the right end. If nums[mid] < nums[high], the right half is sorted and the minimum is at mid or to its left; otherwise the drop — and the minimum — is to the right of mid.
Find Minimum in Rotated Sorted Array (LeetCode 153) कैसे हल करें?
1. low < high loop. 2. Right half sorted → high = mid (mid may be the min). 3. Otherwise low = mid + 1. 4. Return nums[low].
Find Minimum in Rotated Sorted Array की time और space complexity क्या है?
Time: O(log n)। Space: O(1)। (Accepted approach: Binary search on the drop।)
Find Minimum in Rotated Sorted Array में आम गलतियाँ क्या हैं?
Compare with high, not low — comparing with low fails on an unrotated array.