Advertisement
Khadija_Assem

Untitled

Jan 16th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import javafx.util.Pair;
  2. class Solution {
  3. public int[][] kClosest(int[][] points, int K) {
  4. Pair<Integer,Double>[] pairs = new Pair[points.length];
  5. PriorityQueue<Double> minHeap = new PriorityQueue<>();
  6. for(int i=0;i<points.length;i++){
  7. double len = Math.sqrt((Math.pow(points[i][0],2)+Math.pow(points[i][1],2)));
  8. pairs[i] = new Pair<>(i,len);
  9. minHeap.add(len);
  10. }
  11. int[][] p = new int[K][2];
  12. for (int i =0;i<K;i++) {
  13. double len = minHeap.poll();
  14. for(int j=0;j<pairs.length;j++)
  15. if (len == pairs[j].getValue()) {
  16. p[i] = points[pairs[j].getKey()];
  17. pairs[j] = new Pair<>(-1,-1.0);
  18. break;
  19. }
  20. }
  21. return p;
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement