Change priorityQueue to max priorityqueue

后端 未结 17 1847
一整个雨季
一整个雨季 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:00

    Here is a sample Max-Heap in Java :

    PriorityQueue pq1= new PriorityQueue(10, new Comparator() {
    public int compare(Integer x, Integer y) {
    if (x < y) return 1;
    if (x > y) return -1;
    return 0;
    }
    });
    pq1.add(5);
    pq1.add(10);
    pq1.add(-1);
    System.out.println("Peek: "+pq1.peek());
    

    The output will be 10

提交回复
热议问题