Skip to content

added solution to 0973 #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions solution/0973.K Closest Points to Origin/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*;

/**
* @author Furaha Damien
*/

class Solution {

// Helper inner class
public class Point {
int x;
int y;
int distance;

public Point(int x, int y, int distance) {
this.x = x;
this.y = y;
this.distance = distance;
}
}

public int[][] kClosest(int[][] points, int K) {

PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
int[][] res = new int[K][2];

for (int[] temp : points) {
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
que.offer(new Point(temp[0], temp[1], dist));
}
for (int i = 0; i < K; i++) {
Point curr = que.poll();
res[i][0] = curr.x;
res[i][1] = curr.y;
}
return res;

}
}