3

I was solving a problem on leetcode and someone told me that instantiation of a PriorityQueue with a collection takes O(n) time and the addAll method takes O(nlogn) time. Is this correct? I could not find any proofs for this.

https://www.techiedelight.com/find-kth-largest-element-array/ this website on the 3rd approach uses addAll(List) to create a PriorityQueue is it really O(n)?

yaseco
  • 1,094
  • 14
  • 46
Ankit Sharma
  • 1,035
  • 11
  • 15

1 Answers1

0

PriorityQueue in java gets the addAll method from the AbstractQueue. The documentation says that the addAll method just iterates through the supplied Collection. So I'd say addAll takes O(n) time. Take a look at the source code if you're not convinced.

Barath Sankar
  • 176
  • 2
  • 12