-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFGPOTD01.java
More file actions
39 lines (33 loc) · 1021 Bytes
/
Copy pathGFGPOTD01.java
File metadata and controls
39 lines (33 loc) · 1021 Bytes
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
import java.util.PriorityQueue;
class Solution {
static class Point implements Comparable<Point> {
int x;
int y;
int distSq;
int idx;
public Point(int x, int y, int distSq, int idx) {
this.x = x;
this.y = y;
this.distSq = distSq;
this.idx = idx;
}
@Override
public int compareTo(Point p2) {
return this.distSq - p2.distSq;
}
}
public int[][] kClosest(int[][] points, int k) {
PriorityQueue<Point> pq = new PriorityQueue<>();
for (int i = 0; i < points.length; i++) {
int distSq = points[i][0] * points[i][0] + points[i][1] * points[i][1];
pq.add(new Point(points[i][0], points[i][1], distSq, i));
}
int[][] ans = new int[k][2];
for (int i = 0; i < k; i++) {
Point closest = pq.remove();
ans[i][0] = closest.x;
ans[i][1] = closest.y;
}
return ans;
}
}