forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_973.java
39 lines (30 loc) · 986 Bytes
/
_973.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.fishercoder.solutions;
import java.util.PriorityQueue;
public class _973 {
public static class Solution1 {
public int[][] kClosest(int[][] points, int k) {
int[][] ans = new int[k][2];
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> {
double dist1 = getDistance(o1);
double dist2 = getDistance(o2);
if (dist1 > dist2) {
return 1;
} else if (dist1 < dist2) {
return -1;
} else {
return 0;
}
});
for (int[] point : points) {
pq.add(point);
}
for (int i = 0; i < k; i++) {
ans[i] = pq.poll();
}
return ans;
}
private double getDistance(int[] point) {
return Math.sqrt(Math.pow(point[0], 2) + Math.pow(point[1], 2));
}
}
}