且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有可能删除从排序列表重复少于O(n)的时间?

更新时间:2023-02-13 23:39:15

在一般情况下,没有。试想ñ副本的列表。你将不得不作出N-1的清除,因此O(N)。

In general, no. Imagine a list of N duplicates. You would have to make N-1 removals, hence O(N).

如果你为O更好的(1)清除元素指定一个特定的数据结构,则可能对某些种类的投入更好的办法。

If you specify a particular data structure with better than O(1) removal of elements, then there might better way for certain sorts of inputs.

即使你能有效地去除范围在O(1)的元素,它需要O(1)时间来找到一个重复的 - 想象一个列表,其中有N /2对重复的。你还要做N / 2搜索并删除N / 2的范围,这两者都是O(N)。

Even if you can efficiently remove a range of elements in O(1), and it takes O(1) time to find a duplicate - imagine a list where there are N/2 pairs of duplicates. You'll still have to do N/2 searches and remove N/2 ranges, both of which are O(N).

(有也有点含糊不清的问题,标题是删除重复项,但身体是具体到删除一个范围)

(there's also a bit of ambiguity as the question title is 'remove duplicates', but the body is specific to removing one range)

如果从排序所得的列表具有下列再presentation - 每个节点都有一个值,和一个出现计数为该,然后除去重复的一个值将平凡设置计数为1为该节点。 (A 跳跃列表可能有相似的特点,假设一个体面的垃圾回收环境,那里没有成本回收内存),从而使是O(1)一重复。如果您需要删除的所有的从列表中重复,它仍然是O(N)。

If the list resulting from your sort has the following representation - each node has a value, and an occurrence count for that, then removing the duplications for one value will trivially set the count to 1 for that node. ( A skip list probably has similar characteristics, assuming a decent garbage collected environment where there's no cost to reclaiming memory), so that would be O(1) for one duplication. If you need to remove all duplicates from the list, it would still be O(N).