[LintCode] K Closest Points

Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin.
Return these points sorted by distance, if they are same with distance, sorted by x-axis, otherwise sorted by y-axis.

Example

Given points = [[4,6],[4,7],[4,4],[2,5],[1,1]], origin = [0, 0], k = 3
return [[1,1],[2,5],[4,4]]

 
 
Solution 1. PriorityQueue with Comparable Interface 
 1 /**
 2  * Definition for a point.
 3  * class Point {
 4  *     int x;
 5  *     int y;
 6  *     Point() { x = 0; y = 0; }
 7  *     Point(int a, int b) { x = a; y = b; }
 8  * }
 9  */
10 class PointWithDistance implements Comparable<PointWithDistance> {
11     protected Point p;
12     protected long d;
13     public PointWithDistance(Point p, long d)
14     {
15         this.p = p;
16         this.d = d;
17     }
18     public int compareTo(PointWithDistance b)
19     {
20             int ret = 0;
21             if(this.d < b.d)
22             {
23                 ret = -1;
24             }
25             else if(this.d > b.d)
26             {
27                 ret =  1;
28             }
29             else
30             {
31                 if(this.p.x < b.p.x)
32                 {
33                     ret = -1;
34                 }
35                 else if(this.p.x > b.p.x)
36                 {
37                     ret = 1;
38                 }
39                 else if(this.p.y < b.p.y)
40                 {
41                     ret = -1;
42                 }
43                 else if(this.p.y > b.p.y)
44                 {
45                     ret = 1;
46                 }
47                 
48             }
49             return ret;
50     }
51 }
52 public class Solution {
53     /**
54      * @param points a list of points
55      * @param origin a point
56      * @param k an integer
57      * @return the k closest points
58      */
59     public Point[] kClosest(Point[] points, Point origin, int k) {
60         if(points == null || points.length == 0 || k <= 0)
61         {
62             return new Point[0];
63         }
64         Point[] results = new Point[k];
65         PriorityQueue<PointWithDistance> pq = new PriorityQueue<PointWithDistance>(points.length);
66         for(int i = 0; i < points.length; i++)
67         {
68             long dist = calcDist(origin, points[i]);
69             pq.add(new PointWithDistance(points[i], dist));
70         }
71         
72         for(int i = 0; i < k; i++)
73         {
74             results[i] = pq.poll().p;
75         }
76         return results;
77     }
78     private long calcDist(Point a, Point b)
79     {
80         return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
81     }
82 }

 

 
Solution 2. PriorityQueue with the Comparator class
 1 public class Solution {
 2     public Point[] kClosest(Point[] points, Point origin, int k) {
 3         if(points == null || points.length == 0 || k <= 0)
 4         {
 5             return new Point[0];
 6         }
 7         Comparator<Point> comp = new Comparator<Point>(){
 8             public int compare(Point p1, Point p2) {
 9                 long dist1 = calcDist(p1, origin);
10                 long dist2 = calcDist(p2, origin);
11                 if(dist1 < dist2) {
12                     return -1;
13                 }
14                 else if(dist1 > dist2) {
15                     return 1;
16                 }
17                 else if(p1.x < p2.x) {
18                     return -1;
19                 }
20                 else if(p1.x > p2.x) {
21                     return 1;
22                 }
23                 else if(p1.y < p2.y) {
24                     return -1;
25                 }
26                 else if(p1.y > p2.y) {
27                     return 1;
28                 }
29                 return 0;
30             }
31         };
32         Point[] results = new Point[k];
33         PriorityQueue<Point> pq = new PriorityQueue<Point>(points.length, comp);
34         for(int i = 0; i < points.length; i++)
35         {
36             pq.add(points[i]);
37         }
38         
39         for(int i = 0; i < k; i++)
40         {
41             results[i] = pq.poll();
42         }
43         return results;
44     }
45     private long calcDist(Point a, Point b)
46     {
47         return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
48     }
49 }

 

 
 
Related Problems
K Closest Numbers In Sorted Array
 
posted @ 2017-11-13 03:15  Review->Improve  阅读(337)  评论(0编辑  收藏  举报