K Closest Points Lintcode

Posted 璨璨要好好学习

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K Closest Points Lintcode相关的知识,希望对你有一定的参考价值。

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]]

 

这道题思路很简单,debug de了好久真是好气哦!算距离的时候一开始用了Math.sqrt()这个函数,转成double,减完之后转成int,但是这样的话9.05 - 8.78转完了就是0,heap里面就会乱排。。。我以为heap排序不工作了呢。。。想了半天是怎么回事。。。这样看来还是不要遇到问题就怀疑人生。。。一般要从最基本的问题找起。

精度真是要注意再注意啊。。。

另外其实不用开根号的,这样就不会有损失精度的问题了。。。因为开根号可能不一样的减成0,然后又乱排了。。。

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    /**
     * @param points a list of points
     * @param origin a point
     * @param k an integer
     * @return the k closest points
     */
    Point origin;
    public Point[] kClosest(Point[] points, Point original, int k) {
        origin = original;
        PriorityQueue<Point> pq = new PriorityQueue<>(k, new Comparator<Point>() {
            public int compare(Point a, Point b) {
                int disa = (int) (Math.pow((a.x - origin.x),2) + Math.pow((a.y - origin.y), 2));
                int disb = (int) (Math.pow((b.x - origin.x),2) + Math.pow((b.y - origin.y), 2));
                int sub = disb - disa;
                if (sub == 0) {
                    sub = b.x - a.x;
                    if (sub == 0) {
                        sub = b.y - a.y;
                    }
                }
                return sub;
            }
        });
        for (int i = 0; i < points.length; i++) {
            if (pq.size() < k) {
                pq.offer(points[i]);
            } else {
                double disThis = (int) (Math.pow((points[i].x - origin.x),2) + Math.pow((points[i].y - origin.y), 2));
                Point peeked = pq.peek();
                double qPeek = (int) (Math.pow((peeked.x - origin.x),2) + Math.pow((peeked.y - origin.y), 2));
                if (disThis < qPeek) {
                    pq.poll();
                    pq.offer(points[i]);
                }
            }
        }
        Point[] result = new Point[k];
        int i = k - 1;
        while (!pq.isEmpty()) {
            result[i] = pq.poll();
            i--;
        }
        return result;
    }
}

另外其实不用这么麻烦,直接控制到k的个数就可以了。

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    /**
     * @param points a list of points
     * @param origin a point
     * @param k an integer
     * @return the k closest points
     */
    Point origin;
    public Point[] kClosest(Point[] points, Point original, int k) {
        origin = original;
        PriorityQueue<Point> pq = new PriorityQueue<>(k, new Comparator<Point>() {
            public int compare(Point a, Point b) {
                int disa = (int) (Math.pow((a.x - origin.x),2) + Math.pow((a.y - origin.y), 2));
                int disb = (int) (Math.pow((b.x - origin.x),2) + Math.pow((b.y - origin.y), 2));
                int sub = disb - disa;
                if (sub == 0) {
                    sub = b.x - a.x;
                    if (sub == 0) {
                        sub = b.y - a.y;
                    }
                }
                return sub;
            }
        });
        for (int i = 0; i < points.length; i++) {
            pq.offer(points[i]);
            if (pq.size() > k) {
                pq.poll();
            }
        }
        Point[] result = new Point[k];
        int i = k - 1;
        while (!pq.isEmpty()) {
            result[i] = pq.poll();
            i--;
        }
        return result;
    }
}

 

以上是关于K Closest Points Lintcode的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 973. K Closest Points to Origin

K closest points

LC 973. K Closest Points to Origin

[LC] 973. K Closest Points to Origin

973. K Closest Points to Origin - Medium

[LeetCode] 973. K Closest Points to Origin