[英]Array with specific values
Given an array the size of n where: 1/2 of the array is with a single (unknown) value. 给定一个n的大小的数组,其中:1/2的数组具有单个(未知)值。 1/4 of the array is with a single (unknown) different value.
阵列的1/4具有单个(未知)不同的值。 And so on for 1/8, 1/16, 1/32 Give an algorithm to sort the array.
等等为1 / 8,1 / 16,1 / 32给出一个算法来对数组进行排序。 You cannot use the find median algorithm
您不能使用查找中值算法
So what I figured is: There are only logn different values There is a simple solution using a binary heap on O ( n*loglogn) It looks like a question that needed to be solved in O (n) 所以我想的是:只有logn不同的值有一个简单的解决方案在O上使用二进制堆(n * loglogn)它看起来像是一个需要在O(n)中解决的问题
Here is one possible approach: 这是一种可能的方法:
The whole algorithm thus runs in amortized O(n) time, ie, it is dominated by eliminating duplicates and expanding the sorted array. 因此,整个算法以摊销的O(n)时间运行,即,它通过消除重复和扩展排序的阵列来主导。 The space complexity is O(n).
空间复杂度为O(n)。
This is essentially optimal because you need to "touch" all the elements to print the sorted array, which means we have a matching lower bound of Omega(n) on the running time. 这基本上是最佳的,因为您需要“触摸”所有元素以打印排序的数组,这意味着我们在运行时间上具有匹配的下限Omega(n)。
我们的想法是使用多数算法,该算法取O(n)然后发现什么是“半”值从数组中删除它然后再在新数组上再做n + n / 2 + n / 4 + n / 8 + ..... <2n => O(n)
Going over the array once, keep hash map for seen values. 遍历数组一次,保留哈希映射以查看值。 Like you said there are only
log(n)
different values. 就像你说的那样,只有
log(n)
不同的值。
Now you have list of all the different values - sorting them will take lon(n)*log(log(n))
现在你有了所有不同值的列表 - 对它们进行排序将需要
lon(n)*log(log(n))
Once you have the sorted uniq like it's easy to constract the original array : The max value will take n/2
cells , the 2nd take n/4
and so on. 一旦你有了排序的uniq,就像它很容易构建原始数组:最大值将需要
n/2
单元格,第二个需要n/4
,依此类推。
The Total run time is O(n + lon(n)*log(log(n)) + n)
which is O(n)
总运行时间为
O(n + lon(n)*log(log(n)) + n)
,即O(n)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.