Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 4 · Two Sorted Arrays / Partition

Median of Two Sorted Arrays

LeetCode 4 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Binary search the partition: l1 ≤ r2 and l2 ≤ r1.

கடினம்ArrayBinary SearchDivide and Conquer
LeetCode இல் திற ↗

நான் 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. 1

    Brute force: merge-walk to the middle positions.

  2. 2

    Optimal: search mid1 = elements taken from nums1 (the smaller array).

  3. 3

    mid2 = half − mid1; read the border values l1, l2, r1, r2 (±∞ when empty).

  4. 4

    Valid split → median from max(lefts) / min(rights).

  5. 5

    l1 > r2 → take fewer from nums1; else take more.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Merge walkO(m+n) time · O(1) space

Count to the middle.

2
Partition binary searchAcceptedO(log min(m,n)) time

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 element1
Partition 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 -1
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Median 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.