问题
I've the following code and I'm trying to get the time complexity.
seen = set()
a=[4,4,4,3,3,2,1,1,1,5,5]
result = []
for item in a:
if item not in seen:
seen.add(item)
result.append(item)
print (result)
As far as my understanding goes as I'm accessing the list the time complexity for that operation would be O(n)
. As with the if block each time I've a lookup to the set and that would cost another O(n)
. So is the overall time complexity O(n^2)
? Does the set.add()
also add to the complexity?
Also, with the space complexity is it O(n)
? Because the size of the set increases each time it encounters a new element?
Any input or links to get a proper insight into time and space complexities is appreciated.
回答1:
Sets in Python are implemented as hash tables (similar to dictionaries), so both in
and set.add()
are O(1). list.append()
is also O(1) amortized.
Altogether, that means the time complexity is O(n), due to the iteration over a
.
Space complexity is O(n) as well, because the maximum space required is proportional to the size of the input.
A useful reference for the time complexity of various operations on Python collections can by found at https://wiki.python.org/moin/TimeComplexity … and the PyCon talk The Mighty Dictionary provides an interesting delve into how Python achieves O(1) complexity for various set and dict operations.
来源:https://stackoverflow.com/questions/42710045/time-and-space-complexity-for-removing-duplicates-from-a-list