简繁EN

LeetCodeNo. 033

【LeetCode with Python】 4. Median of Two Sorted Arrays

目录

题目

原题页面:https://leetcode.com/problems/median-of-two-sorted-arrays/
题目类型:Divide and Conquer, Array, Binary Search
难度评价:Hard

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)).


分析

找两个有序数列的中位数。发现LeetCode上还是有不少坑。如,这个题目说的有序数列其实是从小到大排列的,不需要我们像处理其它经常埋坑的题那样“以小人之心度君子之腹”地去先检查A和B是从小到大还是从大到小,甚至有可能A从小到大而B却从大到小。

题目里的“median”就是普通的中位数:A和B合起来m+n个数,从小到大排好取正中间那个,m+n是偶数的话正中间有两个,就取它们的平均数。

题目要求时间复杂度是O(log(m+n)),所以不能像归并那样从两个数组开头一个一个往后数。那样要走(m+n)/2步左右,是O(m+n)。我的办法是把求中位数换成在A和B里求第k小的数(k从1数起),就是代码里的findKth(A, B, k)。设total是m+n,total/2是整除。中位数就是第total/2+1小的数;只有total是偶数时,要改成它和第total/2小的数取平均,所以这时调两次findKth。

findKth的思路是每一轮丢掉一批肯定不是答案的数,k也跟着减去丢掉的个数。某个数组丢空了,第k小就是另一个数组剩下部分里的第k个数;k变成1了,就是两个数组剩下部分开头那两个数里小的那个。start_a、start_b记着A、B里还没被丢掉的部分从哪里开始。

  1. 每一轮取half = k/2,在A剩下的部分里看第half个数,下标是index_a,B同理得到index_b。某个数组剩下的不到half个,就看它剩下部分的最后一个数。
  2. 然后比较A[index_a]和B[index_b]:如果A[index_a] <= B[index_b],A剩下部分从开头到index_a的这些数一定都排在第k小的数前面,整段丢掉,start_a移到index_a+1,k减去丢掉的个数。反过来就丢B那一段。

以A[index_a] <= B[index_b]为例,设A剩下部分从开头到index_a有x个数,B剩下部分从开头到index_b有y个数,x和y都不超过half,所以x+y不超过k。排在A[index_a]前面的,最多是A里它前面的x-1个,加上B里B[index_b]前面的y-1个,所以A[index_a]最多排第x+y-1位,也就是不超过第k-1位。A剩下部分到index_a为止的这x个数就都不会是第k小,丢掉不影响答案。

每一轮要么丢掉half个数、k差不多减半,要么把一个数组整个丢空,下一轮直接返回。k最大是total/2+1,所以循环最多O(log(m+n))轮,每一轮只是O(1)的比较和下标运算。两次加起来仍是O(log(m+n)),也只用O(1)的额外空间。丢掉一段只是移动start_a、start_b这两个下标,不要用切片(比如A[index_a+1:])。Python的切片会把切出来的部分复制成一个新列表,复制本身就是线性的。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution:

# @param A, a sorted list
# @param B, a sorted list
# @param k, an integer, 1 <= k <= len(A) + len(B)
# @return the k-th smallest number of A and B together
def findKth(self, A, B, k):

len_a = len(A)
len_b = len(B)
start_a = 0
start_b = 0
while True:
if start_a >= len_a:
return B[start_b + k - 1]
if start_b >= len_b:
return A[start_a + k - 1]
if 1 == k:
return min(A[start_a], B[start_b])

# If A[index_a] <= B[index_b], A[index_a] is at most the (k-1)-th
# smallest of the numbers left, so A[start_a..index_a] can all be
# skipped; the same holds for B in the other case.
half = k / 2
index_a = min(start_a + half, len_a) - 1
index_b = min(start_b + half, len_b) - 1
if A[index_a] <= B[index_b]:
k -= index_a - start_a + 1
start_a = index_a + 1
else:
k -= index_b - start_b + 1
start_b = index_b + 1

# @return a float
def findMedianSortedArrays(self, A, B):

len_a = len(A)
len_b = len(B)
len_total = len_a + len_b
if 0 == len_total:
return ''
if 0 == len_a and 1 == len_b:
return B[0]
if 1 == len_a and 0 == len_b:
return A[0]
if 1 == len_a and 1 == len_b:
return float(A[0] + B[0]) / 2

median_index = len_total / 2
is_odd = (1 == len_total % 2)

if is_odd:
median_num = self.findKth(A, B, median_index + 1)
else:
median_num = float(self.findKth(A, B, median_index) + self.findKth(A, B, median_index + 1)) / 2

return median_num

本文地址:https://xnerv.wang/leetcode-median-of-two-sorted-arrays/