educative.io

Educative

Max Heap initialization (reverse order comparator)

In SlidingWindowMedian problem solution we are initializing max heap as

 PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());

But in the previous (MedianOfAStream) problem solution max heap is intialized as below

PriorityQueue<Integer>  maxHeap = new PriorityQueue<>((a, b) -> b - a);

What is the difference between initialization? Which option would be recommended for initializing max heap?

1 Like