如何在 O(1) Space 中打印 PriorityQueue 的降序

How to Print decreasing order of the PriorityQueue in O(1) Space

正在解决堆上的问题,我希望使用 PriorityQueue 以降序输出问题。
输入:
1
5 2
12 5 787 1 23
输出:
23 787
想要的输出:
787 23

class GFG {

public static void main(String args[])throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int t = Integer.parseInt(br.readLine());
    while(t-->0) {
        // take array and kth element iput
        String n_k[] = br.readLine().split(" ");
        // store array size in n && kth element to find in k
        int n = Integer.parseInt(n_k[0]);
        int k = Integer.parseInt(n_k[1]);
        // Array String input
        String s[] = br.readLine().split(" ");
        int d[] = new int[n];
        for(int i = 0 ; i < n ; i++) {
            d[i] = Integer.parseInt(s[i]);
        }
        
        PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
        // delete the minimum element in the element and just keep 
        // k greater element in the minHeap
        for(int i = 0 ; i < n; i++) {
            minHeap.add(d[i]);
            // if size of heap increases pop tht last element 
            if(minHeap.size()>k) {
                minHeap.poll();
            }
        }
        // print remaining element
        //HERE IS THE PROBLEM I WANT IT IN " DECREASING ORDER "
        // it gives me Increasing order
        while(minHeap.size() > 0) {
            System.out.print(minHeap.peek()+" ");
            minHeap.poll();
        }
        System.out.println();
    }// end of while
    
    }// end of main
 }// end of class

输入:
1
5 2
12 5 787 1 23
输出:
23787

只需用下面的代码替换您的代码即可。

在声明优先级队列的同时,您可以提供倒序的比较器。

PriorityQueue newHeap = new PriorityQueue(minHeap.size(),Comparator.reverseOrder());

我在这里创建了一个新堆,它将以 reverseOrder 存储所有现有元素,然后按原样执行所有现有操作。

给出了预期的答案。

1
5 2
12 5 787 1 23

O/P
787 23 

这对你有帮助。

  class GFG {

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            String n_k[] = br.readLine().split(" ");
            int n = Integer.parseInt(n_k[0]);
            int k = Integer.parseInt(n_k[1]);
            String s[] = br.readLine().split(" ");
            int d[] = new int[n];
            for (int i = 0; i < n; i++) {
                d[i] = Integer.parseInt(s[i]);
            }

            PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
            for (int i = 0; i < n; i++) {
                minHeap.add(d[i]);
                // if size of heap increases pop tht last element
                if (minHeap.size() > k) {
                    minHeap.poll();
                }
            }
            PriorityQueue<Integer> newHeap = new PriorityQueue(minHeap.size(), Comparator.reverseOrder());
            newHeap.addAll(minHeap);
            while (newHeap.size() > 0) {
                System.out.print(newHeap.peek() + " ");
                newHeap.poll();
            }
            System.out.println();
        }

    }
}

可以使用Stream打印结果:

try (Scanner scan = new Scanner(System.in)) {
    int totalCases = scan.nextInt();

    while (totalCases-- > 0) {
        int n = scan.nextInt();
        int k = scan.nextInt();

        Queue<Integer> minHeap = new PriorityQueue<>(k);

        for (int i = 0; i < n; i++) {
            if (minHeap.size() == k)
                minHeap.remove();

            minHeap.add(scan.nextInt());
        }

        System.out.println(minHeap.stream()
                                  .sorted(Comparator.reverseOrder())
                                  .map(String::valueOf)
                                  .collect(Collectors.joining(" ")));
    }
}