Link: https://leetcode.com/problems/median-of-two-sorted-arrays/
Solution:
Topics: binary search
Intuition
Insane problem. I actually found an o(logn*logm)
solution by searching for “what position would I be in if the array was sorted”. We would binary search through nums1
and for every position mid
we simply search for that element in nums2
. We do both a leftmost and rightmost search. If mid + leftmost == median_pos
or mid + rightmost == median_pos
, then we return nums1[mid]
…and of course handle all the edge cases (which are numerous).
There is a far more clever solution though that is o(log(n+m))
. This solution hinges a key observation: We search for a partition point in nums1
and nums2
such that there are an equal number of elements in the combined left and combined right partitions, and every element in the left is smaller or equal to every element in the right.
Lets look at this visually:
Take a look at nums1
and nums2
. Every number in the left is smaller than every number in the right and there is an equal number of elements in both partitions! Basically, we can search for a partition point in nums1
and nums2
where these properties hold true! If this property is false, then we no longer consider the right half of nums1
as potential partition points. Why? Because moving the pointer further right would only increase the max value of the left partition, but we must find a partition point such that all elements in the left partition are smaller or equal to the right partition!
But how do we search for this partition across two arrays? Well, we only need to search through nums1
, and the position in nums2
can be directly computed because the partition must have either (len(nums1)+len(nums2))//2
elements in the case that len(nums1)+len(nums2)
is even, or (len(nums1)+len(nums2))//2 + 1
in the case of even.
The implementation is a mind-bender, but the main thing is to understand the concept above. Revisit this one a few times to nail the implementation because this seem to be a frequently asked question. I think the implementation is so funky because of the potential for out of bounds errors.
Implementation