学习堆的写法,java里默认是小顶堆,需要声明一下改成大顶堆
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if(k==0 || arr.length ==0) return new int[0];
Queue<Integer> pq = new PriorityQueue<>((v1,v2) -> v2 - v1);
for(int num : arr){
if(pq.size() < k) pq.offer(num);
else if(num < pq.peek()){
pq.poll();
pq.offer(num);
}
}
int[] res = new int[k];
int i=0;
for(int num : pq){
res[i++] = num;
}
return res;
}
}