I have the below code, I wanted to know what is the time complexity of this code when I am using PriorityQueue.
I have a listOfNumbers of size N
Queue<Integer> q = new PriorityQueue<>();
q.addAll(listOfNumbers);
while(q.size()>1) {
q.add(q.poll()+q.poll()); // add sum of 2 least elements back to Queue
}
As per this post : Time Complexity of Java PriorityQueue (heap) insertion of n elements?
O(log n) time for the enqueing and dequeing methods (offer, poll, remove() and add)
Now how to calculate the time when I am adding the element back to Queue again.