I have a collection, I don't know which data structure to use yet for this. I have two functions, add and remove.
Both of the functions need to have similar complexities because they both are as frequently used.
It's either add function will be simple as O(1) and removeMax
will be O(log n) or both o(1) or one of them log n and other o(n).
removeMax
should remove the maximum value and return it, and should be able to use it multiple times, so the next time u call it it removes the next new max value.
Is there a way to do both with O(1) or atleast log n for remove?
If it's a sorted structure (such as TreeSet
), both add
and remove
would require O(logN)
.
If it's not sorted, add
can be implemented in O(1)
but removeMax
would take O(N)
, since you must check all the elements to find the maximum in an unsorted data structure.