-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_123nearBy.java
More file actions
32 lines (31 loc) · 889 Bytes
/
_123nearBy.java
File metadata and controls
32 lines (31 loc) · 889 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
import java.util.PriorityQueue;
public class _123nearBy {
static class Point implements Comparable<Point>{
int x;
int y;
int disSq;
int idx;
Point(int x,int y,int disSq,int idx){
this.x=x;
this.y=y;
this.disSq= disSq;
this.idx=idx;
}
@Override
public int compareTo(Point p2){
return this.disSq-p2.disSq;
}
}
public static void main(String[] args) {
int pts[][]={{3,3},{5,-1},{-2,4}};
int k =2;
PriorityQueue<Point> pq = new PriorityQueue<>();
for(int i=0;i<pts.length;i++){
int disSq = pts[i][0]*pts[i][0]+pts[i][1]*pts[i][1];
pq.add(new Point(pts[i][0],pts[i][1],disSq,i));
}
for(int i = 0;i<k;i++){
System.out.println("C"+pq.remove().idx);
}
}
}