Skip to content

Leetcode Problem Median of Two Sorted arrays #1

@PrashannaPdl

Description

@PrashannaPdl
def findMedianSortedArrays(a, b):
    if len(a) > len(b):
        a, b = b, a
    m, n = len(a), len(b)
    l, h = 0, m
    while l <= h:
        i = (l + h) // 2
        j = (m + n + 1) // 2 - i
        L1 = float('-inf') if i == 0 else a[i-1]
        R1 = float('inf') if i == m else a[i]
        L2 = float('-inf') if j == 0 else b[j-1]
        R2 = float('inf') if j == n else b[j]
        if L1 <= R2 and L2 <= R1:
            if (m+n) % 2 == 0:
                return (max(L1, L2) + min(R1, R2)) / 2.0
            else:
                return max(L1, L2)
        elif L1 > R2:
            h = i - 1
        else:
            l = i + 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions