Skip to content
Advertisement

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 based on the output, it looks like it has

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 comparator, or the elements’ natural ordering So PriorityQueue is an exception and it becomes a FIFO queue

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 their natural order in this case.

Advertisement