Reputation: 4553
I am looking for a Scala (or Java/Guava) collection that supports O(1) access to (and ideally removal of) its minimum element as well as O(log n) insertion and removal of arbitrary elements.
Thoughts?
Upvotes: 0
Views: 219
Reputation: 31214
Implementation note: this implementation provides O(log(n)) time for the enqueuing and dequeuing methods (
offer
,poll
,remove()
andadd
); linear time for theremove(Object)
andcontains(Object)
methods; and constant time for the retrieval methods (peek
,element
, andsize
).This class is a member of the Java Collections Framework.
Upvotes: 5
Reputation: 442
Create your own collection backed by ArrayList. It needs additional fields minimum element and position of minimum element. Update those fields when you add element if that element is new minimum.
Upvotes: -1