由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道题(9)
相关主题
An interview question of finding the median in a moving window.讨论一道题
LinkedIn面经(已跪),攒个rp发个一直没有见过满意答案的题吧
sliding windows中维持topk频繁的query问个design的问题
问大家一个cpp中function pointer的问题Top K in N sorted array
Two-Sigma面经A家电面面经
问个题G家电面的两个题
G家电面题目刚电面完l家,只敲了一道题,看来是挂了。。。
问两道google题面试用C++, heap 怎么办?
相关话题的讨论汇总
话题: heap话题: window话题: data话题: list话题: insert
进入JobHunting版参与讨论
1 (共1页)
f*********d
发帖数: 140
1
走过路过的大牛请指教:
Given a sequence of data (it may have duplicates), a fixed-sized moving
window, move the window at each iteration from the start of the data
sequence, such that the oldest data element is removed from the window and a
new data element is pushed into the window,how to find the median of the
data inside the window at each moving?
g*********e
发帖数: 14401
2
用一个max-min-heap来存储window里面的数
复杂度Nlogk
t*********h
发帖数: 941
3
一个简单的方法就是维护一个Priority queue吧 随时更新

a

【在 f*********d 的大作中提到】
: 走过路过的大牛请指教:
: Given a sequence of data (it may have duplicates), a fixed-sized moving
: window, move the window at each iteration from the start of the data
: sequence, such that the oldest data element is removed from the window and a
: new data element is pushed into the window,how to find the median of the
: data inside the window at each moving?

f*********d
发帖数: 140
4
多谢两位大牛指导:
关键是不知到如何删除
s***5
发帖数: 2136
5
a linked list + a min heap + a max heap
f*********d
发帖数: 140
6
收到~ 多谢大牛!

【在 s***5 的大作中提到】
: a linked list + a min heap + a max heap
l*n
发帖数: 529
7
linked list 这里干嘛用的?用来标记谁该删除?要是在heap中间挖个洞还是挺麻烦的
吧。

【在 s***5 的大作中提到】
: a linked list + a min heap + a max heap
a******e
发帖数: 710
8
求详细解释

【在 s***5 的大作中提到】
: a linked list + a min heap + a max heap
c********e
发帖数: 186
9
你觉得这样行不行:
1)删除: Map记录一个元素在heap有的位置,然后相应删除
2)不删除:假设原数组A
一个min heap,一个max heap,一个数组B,B_i表示A_i放在哪个heap里面,0初始值,
1放min heap, 2放max heap。 另外记录min heap和max heap的size,这个size不是
min heap里面的元素的个数,而是里面有效元素即在window里面元素的个数。然后维持
两个heap里面有效元素个数<=1.当一个元素移出window的时候查看在哪个heap,哪个
heap的size就减一。当然挪动一个元素到另外一个heap要更新B和有效size。当extract
min 或者max,如果已无效,则直接remove, 再取下一个。
k***7
发帖数: 6
10
窗口大小为N
list大小为N,顺序存放数值以及记录元素在heap中的存放的Element address[这个不
随堆的调整而变化]
min heap大小为 N/2
max heap大小为 N-N/2
median为max heap顶上元素,access time O(1)
维护堆:list[0]要被下一个新数n替代
if(list[0]<=median){//list[0] is in max heap
remove(list[0], maxHeap);
if(n>maxHeap.top()) {
insert(n, minHeap);
insert(minHeap.top(), maxHeap);
minHeap.pop();
} else {
insert(n, maxHeap);
}
} else { //list[o] is in min heap
remove(list[0], minHeap);
if(n>maxHeap.top()) {
insert(n, minHeap);
} else {
insert(n, maxHeap);
insert(maxHeap.top(), minHeap);
minHeap.pop();
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
面试用C++, heap 怎么办?Two-Sigma面经
周末上道题问个题
我想说,我的A家电面,绝对是被烙印黑了,两个45分钟两个烙印G家电面题目
twittier的onsite挂了,来问个常见题问两道google题
An interview question of finding the median in a moving window.讨论一道题
LinkedIn面经(已跪),攒个rp发个一直没有见过满意答案的题吧
sliding windows中维持topk频繁的query问个design的问题
问大家一个cpp中function pointer的问题Top K in N sorted array
相关话题的讨论汇总
话题: heap话题: window话题: data话题: list话题: insert