跟踪扩展数组的中位数
面试问题:
编辑如下 给你一个数组。您可以从中创建 2 个堆,一个是最小堆,另一个是最大堆。现在使用这 2 个提供的堆在 O(nlog n) 时间内找到数组的中位数。
更正问题 数字是随机生成的并存储到(扩展)数组中。您将如何跟踪中位数?
解决方案 这个问题可以使用 2 个堆来解决,并且中位数总是可以在 O(1) 时间内访问到。
Interview Question:
Edited Below
You are given an array. You make 2 heaps out of it, one minheap and the other max heap. Now find the median of the array using these 2 provided heaps in O(nlog n) time.
Corrected Question
Numbers are randomly generated and stored into an (expanding) array. How would you keep track of the median?
Solution
This problem can be solved using 2 heaps and the median can always be accessed in O(1) time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是如何使用这两个堆。请注意,我假设您不知道元素的数量,这就是为什么我们必须弹出,直到我们从最小堆中弹出的内容大于或等于从最大堆中弹出的内容为止。请注意,我们返回平均值,因为在像
{1, 2, 3, 4}
这样的集合的情况下,中位数实际上是2.5
(两个“中间”的平均值) “值)。我假设 double 作为值类型,但这显然可以是任何类型。这里:由于弹出的时间复杂度为
O(log n)
,并且我们必须弹出O(n / 2)
个值,所以这是O(n log n)
代码>.Here's how you use both heaps. Note that I'm assuming you don't know the number of elements, and this is why we must pop until we pop something from the min heap that is larger than or equal to what we pop from the max heap. Note that we return the average because in the case of a set like
{1, 2, 3, 4}
the median is actually2.5
(the average of the two "middle" values). I'm assumingdouble
as the value type, but this can obviously be anything. Here:Since popping is
O(log n)
and we have to popO(n / 2)
values, this isO(n log n)
.java 中的一个有效实现,使用 2 个堆,O(n log n)。在任何时候,我都会保持两个堆的大小平衡(即,如果我们输入 n 个元素使得 n%2==1,它们最多相差 1)。获取中位数的时间复杂度为 O(1)。添加新元素的时间复杂度为 O(log n)。
A working implementation in java, using 2 heaps, O(n log n). At any time I keep the two heaps balanced in size (ie. they differ at most by 1, if we entered n elements such that n%2==1). Getting the median is O(1). Adding a new element is O(log n).
从堆中弹出是一个 O(log N) 操作,因此您可以通过从其中一个堆中弹出一半元素并获取最后弹出的值来实现 O(N log N)(您必须处理边缘情况)。但这并没有利用其他堆。
您可以使用选择算法实现O(N),但常数因子非常高。如果您已经有堆,前一个建议可能会更好。
Popping from a heap is an O(log N) operation, so you can achieve O(N log N) by popping half the elements from one of the heaps and taking the last popped value (you'd have to handle edge cases). This doesn't take advantage of the other heap though.
You can achieve O(N) using the selection algorithm, but the constant factor is very high. The former suggestion is probably better if you already have a heap.
这是它的 python 实现。我测试了使用 Random.org 并在 中值查找器 其中大多数似乎都有效。
Here's a python implementation for it. I tested some of the examples generated using Random.org and tested them on median finder most of them seem to work.
使用两个堆的 JavaScript 解决方案:
JavaScript solution using two heaps: