Median of Two Sorted Arrays
LeetCode 4 · कठीण · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू
Binary search the partition: l1 ≤ r2 and l2 ≤ r1.
मी Median of Two Sorted Arrays (LeetCode 4) असा सोडवला — Two Sorted Arrays / Partition पॅटर्नमधील एक कठीण प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.
खालील विवरण इंग्रजीत आहे.
Intuition
The median splits the combined data into two equal halves. Instead of merging, binary search how many elements the left half takes from the smaller array; the rest come from the other. The split is correct when everything on the left is ≤ everything on the right.
Approach
- 1
Brute force: merge-walk to the middle positions.
- 2
Optimal: search mid1 = elements taken from nums1 (the smaller array).
- 3
mid2 = half − mid1; read the border values l1, l2, r1, r2 (±∞ when empty).
- 4
Valid split → median from max(lefts) / min(rights).
- 5
l1 > r2 → take fewer from nums1; else take more.
मी कसा सोडवला · पहिल्या कल्पनेपासून अंतिम उपायापर्यंत
Count to the middle.
Find the perfect split.
Merge walkPythonO(m+n) time · O(1) space
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
#brute
m = len(nums1)
n = len(nums2)
mid1 = (m+n)//2
mid2 = mid1-1
element1=None
element2=None
l=0
r=0
count=0
while l<m and r<n:
if nums1[l]<=nums2[r]:
if count==mid1:
element1 = nums1[l]
if count==mid2:
element2 = nums1[l]
l+=1
else:
if count==mid1:
element1 = nums2[r]
if count==mid2:
element2 = nums2[r]
r+=1
count+=1
while l<m:
if count==mid1:
element1 = nums1[l]
if count==mid2:
element2 = nums1[l]
count+=1
l+=1
while r<n:
if count==mid1:
element1 = nums2[r]
if count==mid2:
element2 = nums2[r]
count+=1
r+=1
if (m+n)%2==0:
return (element1+element2)/2
else:
return element1Partition binary searchPythonO(log min(m,n)) time
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
#brute
if len(nums1)>len(nums2):
nums1,nums2 = nums2,nums1
m = len(nums1)
n = len(nums2)
no_of_elements_req_left=(m+n)//2
low = 0
high = m
while low<=high:
mid1 = (low+high)//2 #no of elements on left
mid2 = no_of_elements_req_left-mid1
l1=nums1[mid1-1] if mid1>0 else float("-inf") #last element taken from nums1
l2=nums2[mid2-1] if mid2>0 else float("-inf") #last element taken from nums2
r1=nums1[mid1] if mid1<m else float("inf") #first element from nums1
r2=nums2[mid2] if mid2<n else float("inf") #first element from nums2
if l1<=r2 and l2<=r1:
return (max(l1,l2)+min(r1,r2))/2 if (m+n)%2==0 else min(r1,r2)
elif l1>r2:
high = mid1-1
else:
low = mid1+1
return -1Median of Two Sorted Arrays — वारंवार विचारले जाणारे प्रश्न
Median of Two Sorted Arrays मागील intuition काय आहे?
The median splits the combined data into two equal halves. Instead of merging, binary search how many elements the left half takes from the smaller array; the rest come from the other. The split is correct when everything on the left is ≤ everything on the right.
Median of Two Sorted Arrays (LeetCode 4) कसा सोडवावा?
1. Brute force: merge-walk to the middle positions. 2. Optimal: search mid1 = elements taken from nums1 (the smaller array). 3. mid2 = half − mid1; read the border values l1, l2, r1, r2 (±∞ when empty). 4. Valid split → median from max(lefts) / min(rights). 5. l1 > r2 → take fewer from nums1; else take more.
Median of Two Sorted Arrays ची time आणि space complexity किती आहे?
Time: O(log min(m, n)). Space: O(1). (Accepted approach: Partition binary search.)
Median of Two Sorted Arrays मध्ये सामान्य चुका कोणत्या?
Search over the shorter array, or mid2 can go out of range. Use ±∞ sentinels for empty sides.
Median of Two Sorted Arrays सोडवण्याचे एकापेक्षा जास्त मार्ग आहेत का?
Merge walk (O(m+n) time · O(1) space): Count to the middle. Partition binary search (O(log min(m,n)) time): Find the perfect split.