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.