Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f936783

Browse files
Merge branch 'master' of https://github.com/doocs/leetcode into dev
2 parents 5d8c24f + 9c00603 commit f936783

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
3+
/**
4+
* @author Furaha Damien
5+
*/
6+
7+
class Solution {
8+
9+
// Helper inner class
10+
public class Point {
11+
int x;
12+
int y;
13+
int distance;
14+
15+
public Point(int x, int y, int distance) {
16+
this.x = x;
17+
this.y = y;
18+
this.distance = distance;
19+
}
20+
}
21+
22+
public int[][] kClosest(int[][] points, int K) {
23+
24+
PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
25+
int[][] res = new int[K][2];
26+
27+
for (int[] temp : points) {
28+
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
29+
que.offer(new Point(temp[0], temp[1], dist));
30+
}
31+
for (int i = 0; i < K; i++) {
32+
Point curr = que.poll();
33+
res[i][0] = curr.x;
34+
res[i][1] = curr.y;
35+
}
36+
return res;
37+
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /