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

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

转载于:https://www.cnblogs.com/jxlgetter/p/4395118.html

你可能感兴趣的:(面试)