Change priorityQueue to max priorityqueue

后端 未结 17 1850
一整个雨季
一整个雨季 2020-12-04 05:13

I have priority queue in Java of Integers:

 PriorityQueue pq= new PriorityQueue();

When I call pq.poll(

17条回答
  •  难免孤独
    2020-12-04 06:15

    I just ran a Monte-Carlo simulation on both comparators on double heap sort min max and they both came to the same result:

    These are the max comparators I have used:

    (A) Collections built-in comparator

     PriorityQueue heapLow = new PriorityQueue(Collections.reverseOrder());
    

    (B) Custom comparator

    PriorityQueue heapLow = new PriorityQueue(new Comparator() {
        int compare(Integer lhs, Integer rhs) {
            if (rhs > lhs) return +1;
            if (rhs < lhs) return -1;
            return 0;
        }
    });
    

提交回复
热议问题