Skip to content

Commit 1bae725

Browse files
authored
Create Solution2.java
1 parent 4183994 commit 1bae725

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int[][] kClosest(int[][] points, int k) {
3+
PriorityQueue<int[]> maxQ = new PriorityQueue<>((a, b) -> b[0] - a[0]);
4+
for (int i = 0; i < points.length; ++i) {
5+
int x = points[i][0], y = points[i][1];
6+
maxQ.offer(new int[] {x * x + y * y, i});
7+
if (maxQ.size() > k) {
8+
maxQ.poll();
9+
}
10+
}
11+
int[][] ans = new int[k][2];
12+
for (int i = 0; i < k; ++i) {
13+
ans[i] = points[maxQ.poll()[1]];
14+
}
15+
return ans;
16+
}
17+
}

0 commit comments

Comments
 (0)