educative.io

Educative

Using comparators on priority queue

Hey folks, I’m struggling with the comparator logic alongs the likes of:

PriorityQueue<Node> minHeap = new PriorityQueue<Node>( (n1, n2) -> lists.get(n1.arrayIndex)[n1.elementIndex] - lists.get(n2.arrayIndex)[n2.elementIndex]);

Has anyone any good resources for learning on this? How can I learn the likes of these key words arrayIndex and elementIndex. As this is becoming a real sticking point for moving forward with these kinds of questions.

Cheers

This is how I interpret:

List is part of code.
Break the code:
Integer[] temp= lists.get(node.arrayIndex);
Integer result=temp[node.elementIndex];

Need to implement above piece of code for n1 (elementIndex,arrayIndex) and n2 (elementIndex,arrayIndex) we get result 1 and result 2, consider small value of result from both the values,
and we finally consider equivalent result node on the top of heap.

1 Like