Median of Two Sorted Arrays

Problem

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Intuition

The problem requires finding the median of two sorted arrays efficiently with an overall runtime complexity of O(log(m + n)). The solution employs a binary search approach to partition the arrays and find the correct position of the median.

Approach

Initialize two arrays, a and b, such that a is the smaller array among nums1 and nums2. The goal is to find the partition point in a (represented by index i) that divides the combined arrays into two halves.
Calculate the total number of elements in both arrays (total = len(nums1) + len(nums2)) and the index corresponding to the middle element (half = total // 2).
Perform a binary search on the smaller array (a) using pointers left and right. The goal is to find the correct partition point i in a.
Calculate the corresponding index j in the larger array (b) based on the total length and the partition point in a.
Determine the values of elements on both sides of the partition point in both arrays (aleft, aright, bleft, bright).
Check if the partition is correct by comparing aleft, bright, bleft, and aright.
If the condition aleft <= bright and bleft <= aright is satisfied, it means the partition is correct, and the median is found.
If total is odd, return the minimum of aright and bright.
If total is even, return the average of max(aleft, bleft) and min(aright, bright).
If the partition is not correct, adjust the pointers (left or right) based on the comparison results.
If aleft > bright, move the right pointer to i - 1.
If aleft <= bright, move the left pointer to i + 1.
Repeat the binary search until the correct partition point is found.

Complexity

  • Time complexity:

The time complexity of this solution is O(log(min(m, n))), where m and n are the lengths of the input arrays nums1 and nums2. The binary search efficiently narrows down the search interval.

  • Space complexity:

The space complexity is O(1) since the solution uses only a constant amount of extra space.

Code

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        a , b = nums1 , nums2
        total = len(nums1) + len(nums2)
        half = total // 2

        if len(b) < len(a):
            a , b = b , a

        left , right = 0 , len(a) - 1

        while True:
            i = (left + right) // 2
            j = half - i - 2

            aleft = a[i] if i >= 0 else float('-inf')
            aright = a[i + 1] if (i + 1) < len(a) else float('inf')
            bleft = b[j] if j >= 0 else float('-inf')
            bright = b[j + 1] if (j + 1) < len(b) else float('inf')

            if aleft <= bright and bleft <= aright:
                if total % 2:
                    return min(aright,bright)

                return (max(aleft,bleft) + min(aright , bright)) / 2

            elif aleft > bright:
                right =  i - 1
            
            else:
                left = i + 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值