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.
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-10^6 <= nums1[i], nums2[i] <= 10^6
Can you provide an algorithm that satisfies the logarithmic time complexity?
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))
.
The most straightforward approach is to merge the two sorted arrays into a single sorted array and then find the median.
nums1
and nums2
into a new array merged
. This takes O(m+n) time.merged
array. This takes O((m+n)log(m+n)) time.merged
is odd, the median is the middle element.merged
is even, the median is the average of the two middle elements.def findMedianSortedArrays_brute_force(nums1, nums2):
merged = nums1 + nums2
merged.sort()
n = len(merged)
if n % 2 == 0:
return (merged[n // 2 - 1] + merged[n // 2]) / 2
else:
return float(merged[n // 2])
O((m+n)log(m+n)) due to sorting the merged array.
O(m+n) because we create a new merged array.
To achieve O(log (m+n))
runtime complexity, we can use a binary search approach. The key idea is to find the correct partition in both arrays such that:
(m+n+1)/2
. This ensures we are at the median.Steps:
nums1
is the smaller array. If not, swap them.low = 0
and high = m
(length of nums1
).partitionX = (low + high) // 2
partitionY = (m + n + 1) // 2 - partitionX
maxLeftX
, minRightX
, maxLeftY
, minRightY
(edge cases consider float('-inf')
and float('inf')
)maxLeftX <= minRightY
and maxLeftY <= minRightX
, we found the correct partitions.
(m+n)
is even, return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
(m+n)
is odd, return max(maxLeftX, maxLeftY)
maxLeftX > minRightY
, move high = partitionX - 1
low = partitionX + 1
def findMedianSortedArrays(nums1, nums2):
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
m, n = len(nums1), len(nums2)
low, high = 0, m
while low <= high:
partitionX = (low + high) // 2
partitionY = (m + n + 1) // 2 - partitionX
maxLeftX = nums1[partitionX - 1] if partitionX > 0 else float('-inf')
minRightX = nums1[partitionX] if partitionX < m else float('inf')
maxLeftY = nums2[partitionY - 1] if partitionY > 0 else float('-inf')
minRightY = nums2[partitionY] if partitionY < n else float('inf')
if maxLeftX <= minRightY and maxLeftY <= minRightX:
if (m + n) % 2 == 0:
return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
else:
return float(max(maxLeftX, maxLeftY))
elif maxLeftX > minRightY:
high = partitionX - 1
else:
low = partitionX + 1
return -1 # Should not happen, but included for completeness
O(log(min(m, n))) because we perform binary search on the smaller array.
O(1) because we do not use any extra space.