Median of Two Sorted Arrays

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 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.
Input: nums1 = [0,0], nums2 = [0,0]
Output: 0.00000
Input: nums1 = [], nums2 = [1]
Output: 1.00000
Input: nums1 = [2], nums2 = []
Output: 2.00000
left_A             |           right_A
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
left_B              |        right_B
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
left_part          |        right_part
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
1) len(left_part) == len(right_part)
2) max(left_part) <= min(right_part)
(1) i + j == m - i + n - j (或者: m - i + n - j + 1) 即让左半边元素数量等于与右半边
对于 n >= m 的情况,我们只需要让 : i = 0 ~ m, j = (m + n + 1)/2 - i
(2) B[j-1] <= A[i] 并且 A[i-1] <= B[j] 即让左边最大元素小于右边最小元素
在 [0, m] 中找到一个使下面不等式成立的 i :
B[j-1] <= A[i] and A[i-1] <= B[j], ( where j = (m + n + 1)/2 - i )
<1> 设 imin = 0, imax = m, 然后在这个区间 [imin, imax] 中查找 i<2> 设 i = (imin + imax)/2, j = (m + n + 1)/2 - i<3> 此时,我们满足了 len(left_part)==len(right_part), 我们会遇到三种情况:
<a> B[j-1] <= A[i] and A[i-1] <= B[j]
说明我们找到了我们需要的i,停止搜索。
<b> B[j-1] > A[i]
意味着 A[i] 太小, 那么我们必须调整 i 以使 `B[j-1] <= A[i]` 仍然成立。
我们可以增大 i吗?
Yes. 因为 i 增大时, j 将减小。
所以 B[j-1] 跟着减小而 A[i] 会增大。`B[j-1] <= A[i]`就可能成立。
我们可以减小 i 吗?
No! 因为 i 减小时, j 将增大。
所以 B[j-1] 增大而 A[i] 减小。B[j-1] <= A[i] 永远不可能成立。
所以我们必须增加 i。也就是将搜索范围调整为[i+1, imax]。 所以,设 imin = i+1, 然后回到步骤 <2>.
<c> A[i-1] > B[j]
意味着 A[i-1] 太大。我们必须减小 i 以使 `A[i-1]<=B[j]`.
就是说我们要调整搜索范围为 [imin, i-1].
所以, 设 imax = i-1, 然后回到步骤 <2>.
max(A[i-1], B[j-1]) ( m + n 是奇数)
或者 (max(A[i-1], B[j-1]) + min(A[i], B[j]))/2 ( m + n 是偶数)
在 [0, m] 中找到一个使下面不等式成立的 i :
(j == 0 or i == m or B[j-1] <= A[i]) and
(i == 0 or j == n or A[i-1] <= B[j])
where j = (m + n + 1)/2 - i
<a> (j == 0 or i == m or B[j-1] <= A[i]) and
(i == 0 or j = n or A[i-1] <= B[j])
说明 i 的值满足要求,停止循环
<b> j > 0 and i < m and B[j - 1] > A[i]
说明 i 的值太小, 增加它.
<c> i > 0 and j < n and A[i - 1] > B[j]
说明 i 的值过大, 减小它。
m <= n, i < m ==> j = (m+n+1)/2 - i > (m+n+1)/2 - m >= (2*m+1)/2 - m >= 0    
m <= n, i > 0 ==> j = (m+n+1)/2 - i < (m+n+1)/2 <= (2*n+1)/2 <= n
def median(A, B):
m, n = len(A), len(B)
if m > n:
A, B, m, n = B, A, n, m
if n == 0:
raise ValueError
imin, imax, half_len = 0, m, (m + n + 1) / 2
while imin <= imax:
i = (imin + imax) / 2
j = half_len - i
if i < m and B[j-1] > A[i]:
# i 的值太小, 增加它
imin = i + 1
elif i > 0 and A[i-1] > B[j]:
# i 的值过大, 减小它
imax = i - 1
else:
# i is perfect
if i == 0: max_of_left = B[j-1]
elif j == 0: max_of_left = A[i-1]
else: max_of_left = max(A[i-1], B[j-1])
if (m + n) % 2 == 1:
return max_of_left
if i == m: min_of_right = B[j]
elif j == n: min_of_right = A[i]
else: min_of_right = min(A[i], B[j])
return (max_of_left + min_of_right) / 2.0

经评论区提醒,// 在不同的python版本中有不同的默认行为:
在 Python 2.2 :
要引用: from __future__ import division
“ / “就表示 浮点数除法,返回浮点结果;” // “表示整数除法。

Python 3以后 :
“ / “就表示 浮点数除法,返回浮点结果;” // “表示整数除法。

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store