Given the code below:
pq.offer(x);
pq.poll();
For the first line code, element x is inserted into Priority Queue pq, the time complexity of the offer is log(k), where k is the size of pq.
Then my question is, for the second line code that immediately follows the first line, what'll be the time complexity for poll() ?
After first line offer, the pq has already been sorted, so poll will simply retrieve and remove the head of queue, then I think it should be O(1), correct?
Thanks
PriorityQueueare not sorted. Items are inserted into a binary heap, which is an ordered, but not necessarily stored, data structure.