-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_973.java
69 lines (59 loc) · 2.31 KB
/
_973.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.fishercoder.solutions.firstthousand;
import java.util.PriorityQueue;
public class _973 {
public static class Solution1 {
public int[][] kClosest(int[][] points, int k) {
int[][] ans = new int[k][2];
PriorityQueue<int[]> pq =
new PriorityQueue<>(
(o1, o2) -> {
double dist1 = getDistance(o1);
double dist2 = getDistance(o2);
if (dist1 > dist2) {
return 1;
} else if (dist1 < dist2) {
return -1;
} else {
return 0;
}
});
for (int[] point : points) {
pq.add(point);
}
for (int i = 0; i < k; i++) {
ans[i] = pq.poll();
}
return ans;
}
private double getDistance(int[] point) {
return Math.sqrt(Math.pow(point[0], 2) + Math.pow(point[1], 2));
}
}
public static class Solution2 {
public int[][] kClosest(int[][] points, int k) {
PriorityQueue<int[]> maxHeap =
new PriorityQueue<>(
(a, b) -> (b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1]));
for (int[] point : points) {
long distance = (long) point[0] * point[0] + point[1] * point[1];
if (maxHeap.size() < k) {
maxHeap.offer(point);
} else {
int[] peek = maxHeap.peek();
long peekedDistance = (long) peek[0] * peek[0] + peek[1] * peek[1];
if (peekedDistance > distance) {
// this is an optimization so that the space complexity is limited to O(k)
maxHeap.poll();
maxHeap.offer(point);
}
}
}
int[][] result = new int[k][2];
for (int i = 0; i < k; i++) {
int[] point = maxHeap.poll();
result[i] = point;
}
return result;
}
}
}