最佳答案
这是一个家庭作业问题,二进制搜索已经被引入:
Given two arrays, respectively N and M elements in ascending order, not necessarily unique:
什么是时间有效的算法,以找到 K最小的元素在两个数组的联合?
They say it takes O(logN + logM)
where N
and M
are the arrays lengths.
Let's name the arrays a
and b
. Obviously we can ignore all a[i]
and b[i]
where i > k.
First let's compare a[k/2]
and b[k/2]
. Let b[k/2]
> a[k/2]
. Therefore we can discard also all b[i]
, where i > k/2.
现在我们有了所有的 a[i]
,其中 i < k 和所有的 b[i]
,其中 i < k/2可以找到答案。
下一步是什么?