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.