We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4183994 commit 1bae725Copy full SHA for 1bae725
solution/0900-0999/0973.K Closest Points to Origin/Solution2.java
@@ -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