-
Notifications
You must be signed in to change notification settings - Fork 0
/
KClosestPointsToOrigin.java
53 lines (40 loc) · 1.15 KB
/
KClosestPointsToOrigin.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
package com.leetcode.MayChallenge.week5;
import java.util.Comparator;
import java.util.PriorityQueue;
/*
Time complexity :- O((n-k)logk) , n is the number of points
Space complexity :- O(n) , n is the number of points
*/
public class KClosestPointsToOrigin {
public int[][] kClosest(int[][] points, int K) {
PriorityQueue<int[]> maxHeap = new PriorityQueue<int[]>(new PointComparator());
for (int[] i : points) {
maxHeap.add(i);
if (maxHeap.size() > K)
maxHeap.poll();
}
int result[][] = new int[K][2];
for (int i = 0; i < K; i++) {
int res[] = maxHeap.poll();
result[i][0] = res[0];
result[i][1] = res[1];
}
return result;
}
public static void main(String[] args) {
int points[][] = { { 3, 3 }, { 5, -1 }, { -2, 4 } }, K = 2;
int[][] result = new KClosestPointsToOrigin().kClosest(points, K);
for (int i = 0; i < K; i++) {
System.out.println("" + result[i][0] + result[i][1]);
}
}
}
class PointComparator implements Comparator<int[]> {
@Override
public int compare(int[] p1, int[] p2) {
return Double.compare(dist(p2), dist(p1));
}
private double dist(int[] p) {
return (p[0] * p[0] + p[1] * p[1]);
}
}