// used priorityQueue to find top K
class Solution {
public int[][] kClosest(int[][] points, int k) {
PriorityQueue<int []> q = new PriorityQueue<>( (a, b) -> {
return (a[0]*a[0] + a[1]*a[1]) - (b[0]*b[0] +b[1]*b[1]) ;
});
for(int [] p : points){
q.offer(p);
}
List<int []> ret = new ArrayList<>();
int i = 0;
while(i++ < k){
ret.add(q.poll());
}
return ret.toArray(new int [ret.size()][]);
}
}
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up