Skip to content

Tag: priority-queue

Java – Use PriorityQueue sort list but have empty list

This is my solution for leetcode 759. Employee Free Time, but it has index out of boundary issue that i don’t understand. I got “index 0 out of bounds for length 0” exception. The test case i used is ‘[[[1,2]],[[1,3]],[[4,10]]]’ and this is the output: I only have 3 lists and bas…

Is PriorityQueue a FIFO Queue?

PriorityQueue implements Queue, but is PriorityQueue a FIFO data structure like Queue? Answer From the Queue interface: Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied compara…

Change priorityQueue to max priorityqueue

I have priority queue in Java of Integers: When I call pq.poll() I get the minimum element. Question: how to change the code to get the maximum element? Answer How about like this: The Collections.reverseOrder() provides a Comparator that would sort the elements in the PriorityQueue in a the oposite order to …