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;
    }
}
时间: 2024-08-25 01:20:53

K Closest Points Lintcode的相关文章

K closest points

Find the K closest points to a target point in a 2D plane. import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; class Point { public int x; public int y; public Point(int x, int y) { this.x =

Microsoft - Find the K closest points to the origin in a 2D plane

Find the K closest points to the origin in a 2D plane, given an array containing N points. 用 max heap 做 /* public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } */ public List<Point> findKClosest(P

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

LC 973. K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

[Solution] 973. K Closest Points to Origin

Difficulty: Easy Problem We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order. The answer is guaran

LeetCode 973. K Closest Points to Origin

原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return

973. K Closest Points to Origin - Medium

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

FB面经 Prepare: K closest point to the origin

Give n points on 2-D plane, find the K closest points to origin 1 package fbPractise; 2 3 import java.util.*; 4 5 class Coordinate { 6 int x; 7 int y; 8 public Coordinate(int x, int y) { 9 this.x = x; 10 this.y = y; 11 } 12 } 13 14 public class Kclos

K Closest Numbers In Sorted Array

Given a target number, a non-negative integer k and an integer array A sorted in ascending order, find the k closest numbers to target in A, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending ord