Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.util.Pair;
- class Solution {
- public int[][] kClosest(int[][] points, int K) {
- Pair<Integer,Double>[] pairs = new Pair[points.length];
- PriorityQueue<Double> minHeap = new PriorityQueue<>();
- for(int i=0;i<points.length;i++){
- double len = Math.sqrt((Math.pow(points[i][0],2)+Math.pow(points[i][1],2)));
- pairs[i] = new Pair<>(i,len);
- minHeap.add(len);
- }
- int[][] p = new int[K][2];
- for (int i =0;i<K;i++) {
- double len = minHeap.poll();
- for(int j=0;j<pairs.length;j++)
- if (len == pairs[j].getValue()) {
- p[i] = points[pairs[j].getKey()];
- pairs[j] = new Pair<>(-1,-1.0);
- break;
- }
- }
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement