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(Point[] p, int k) {
    PriorityQueue<Point> pq = new PriorityQueue<>(10, new Comparator<Point>() {
        @Override
        public int compare(Point a, Point b) {
            return (b.x * b.x + b.y * b.y) - (a.x * a.x + a.y * a.y);
        }
    });

    for (int i = 0; i < p.length; i++) {
        if (i < k)
            pq.offer(p[i]);
        else {
            Point temp = pq.peek();
            if ((p[i].x * p[i].x + p[i].y * p[i].y) - (temp.x * temp.x + temp.y * temp.y) < 0) {
                pq.poll();
                pq.offer(p[i]);
            }
        }
    }

    List<Point> x = new ArrayList<>();
    while (!pq.isEmpty())
        x.add(pq.poll());

    return x;
}

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/8970020.html

时间: 2025-01-12 14:52:32

Microsoft - Find the K closest points to the origin in a 2D plane的相关文章

[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

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 =

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. Exam

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