您的位置:首页 > 职场人生

[Google面试]10G的数据如何用2G的空间找到中位数(Median)

2015-03-29 12:57 309 查看
Stack Overflow上面的解答,简直是赞:

Create an 8-byte array with 2^16 entries. Take your input numbers, shift off the bottom sixteen bits, and create a histogram. (That said, each bin is the count of that number)

Now you count up in that histogram until you reach the bin that covers the midpoint of the values.

Pass through again, ignoring all numbers that don’t have that same set of top bits, and make a histogram of the bottom bits.

Count up through that histogram until you reach the bin that covers the midpoint of the (entire list of) values.

Now you know the median, in O(n) time and O(1) space (in practice, under 1 MB).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: