class Solution {
public int[][] kClosest(int[][] points, int K) {
//建立大根堆
PriorityQueue<int[]> maxheap = new PriorityQueue<>((v1,v2)->cal(v2)-cal(v1));
//先插入K个数组
for(int i = 0;i < K;i++){
maxheap.add(points[i]);
}
//插入剩下的数组,遍插遍比较
for(int j = K;j<points.length;j++){
if(cal(points[j])<cal(maxheap.peek())){
maxheap.poll();
maxheap.add(points[j]);
}
}
//定义一个结果数组
int[][] ans = new int[K][2];
for(int n = 0;n < K;n++){
ans[n] = maxheap.poll();
}
return ans;
}
//定义距离公式
public int cal(int a[] ){
return (a[0]*a[0]+a[1]*a[1]);
}
}