There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
-
自己的解题思路
-
别人的解题思路
-
找到一个新的网站,第一印象不错的。 http://www.geeksforgeeks.org
-
这题思路比较难理解,因为有很多边界条件。比如,奇偶的考虑,中间数的考虑。 有待后期再刷一遍。
This problem is notoriously hard to implement due to all the corner cases. Most implementations consider odd-lengthed and even-lengthed arrays as two different cases and treat them separately. As a matter of fact, with a little mind twist. These two cases can be combined as one, leading to a very simple solution where (almost) no special treatment is needed.
First, let's see the concept of 'MEDIAN' in a slightly unconventional way. That is:
"if we cut the sorted array to two halves of EQUAL LENGTHS, then
median is the AVERAGE OF Max(lower_half) and Min(upper_half), i.e. the
two numbers immediately next to the cut".
For example, for [2 3 5 7], we make the cut between 3 and 5:
then the median = (3+5)/2. Note that I'll use '/' to represent a cut, and (number / number) to represent a cut made through a number in this article.
for [2 3 4 5 6], we make the cut right through 4 like this:
[2 3 (4/4) 5 7]
Since we split 4 into two halves, we say now both the lower and upper subarray contain 4. This notion also leads to the correct answer: (4 + 4) / 2 = 4;
For convenience, let's use L to represent the number immediately left to the cut, and R the right counterpart. In [2 3 5 7], for instance, we have L = 3 and R = 5, respectively.
We observe the index of L and R have the following relationship with the length of the array N:
It is not hard to conclude that index of L = (N-1)/2, and R is at N/2. Thus, the median can be represented as
To get ready for the two array situation, let's add a few imaginary 'positions' (represented as #'s) in between numbers, and treat numbers as 'positions' as well.
As you can see, there are always exactly 2*N+1 'positions' regardless of length N. Therefore, the middle cut should always be made on the Nth position (0-based). Since index(L) = (N-1)/2 and index(R) = N/2 in this situation, we can infer that index(L) = (CutPosition-1)/2, index(R) = (CutPosition)/2.
Now for the two-array case:
Similar to the one-array problem, we need to find a cut that divides the two arrays each into two halves such that
"any number in the two left halves" <= "any number in the two right
halves".
We can also make the following observations:
- There are 2N1 + 2N2 + 2 position altogether. Therefore, there must be exactly N1 + N2 positions on each side of the cut, and 2 positions directly on the cut.
- Therefore, when we cut at position C2 = K in A2, then the cut position in A1 must be C1 = N1 + N2 - k. For instance, if C2 = 2, then we must have C1 = 4 + 5 - C2 = 7.
- [# 1 # 2 # 3 # (4/4) # 5 #]
- [# 1 / 1 # 1 # 1 #]
-
When the cuts are made, we'd have two L's and two R's. They are
- L1 = A1[(C1-1)/2]; R1 = A1[C1/2];
- L2 = A2[(C2-1)/2]; R2 = A2[C2/2];
In the above example,
Now how do we decide if this cut is the cut we want? Because L1, L2 are the greatest numbers on the left halves and R1, R2 are the smallest numbers on the right, we only need
to make sure that any number in lower halves <= any number in upper halves. As a matter of fact, since
L1 <= R1 and L2 <= R2 are naturally guaranteed because A1 and A2 are sorted, we only need to make sure:
L1 <= R2 and L2 <= R1.
Now we can use simple binary search to find out the result.
Two side notes:
A. since C1 and C2 can be mutually determined from each other, we might as well select the shorter array (say A2) and only move C2 around, and calculate C1 accordingly. That way we can achieve a run-time complexity of O(log(min(N1, N2)))
B. The only edge case is when a cut falls on the 0th(first) or the 2Nth(last) position. For instance, if C2 = 2N2, then R2 = A2[2*N2/2] = A2[N2], which exceeds the boundary of the array. To solve this problem, we can imagine that both A1 and A2 actually have two extra elements, INT_MAX at A[-1] and INT_MAX at A[N]. These additions don't change the result, but make the implementation easier: If any L falls out of the left boundary of the array, then L = INT_MIN, and if any R falls out of the right boundary, then R = INT_MAX.
I know that was not very easy to understand, but all the above reasoning eventually boils down to the following concise code:
-
一个比较陌生的网站,不过对它的第一印象不错。也是相关题目的题解。 http://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/