📜  使用优先级队列找到最接近原点的K个点

📅  最后修改于: 2021-05-04 07:20:45             🧑  作者: Mango

给定2D平面上n个点的列表,任务是找到最接近原点O(0,0)的K(k 注意:使用标准欧几里得距离,点P(x,y)和O(0,0)之间的距离
例子:

使用基于距离的排序的方法本文这种方法进行了说明。
使用优先级队列进行比较的方法:为了解决上述问题,主要思想是根据点到原点的距离,将点的坐标存储在成对的优先级队列中。为了将最大优先级分配给与原点距离最远的点,我们使用优先级队列中的Comparator类。然后打印优先级队列的前K个元素。

下面是上述方法的实现:

C++
// C++ implementation to find the K
// closest points to origin
// using Priority Queue
  
#include 
using namespace std;
  
// Comparator class to assign
// priority to coordinates
class comp {
  
public:
    bool operator()(pair a,
                    pair b)
    {
        int x1 = a.first * a.first;
        int y1 = a.second * a.second;
        int x2 = b.first * b.first;
        int y2 = b.second * b.second;
  
        // return true if distance
        // of point 1 from origin
        // is greater than distance of
        // point 2 from origin
        return (x1 + y1) > (x2 + y2);
    }
};
  
// Function to find the K closest points
void kClosestPoints(int x[], int y[],
                    int n, int k)
{
    // Create a priority queue
    priority_queue,
                   vector >,
                   comp>
        pq;
  
    // Pushing all the points
    // in the queue
    for (int i = 0; i < n; i++) {
        pq.push(make_pair(x[i], y[i]));
    }
  
    // Print the first K elements
    // of the queue
    for (int i = 0; i < k; i++) {
  
        // Store the top of the queue
        // in a temporary pair
        pair p = pq.top();
  
        // Print the first (x)
        // and second (y) of pair
        cout << p.first << " "
             << p.second << endl;
  
        // Remove top element
        // of priority queue
        pq.pop();
    }
}
  
// Driver code
int main()
{
    // x coordinate of points
    int x[] = { 1, -2 };
  
    // y coordinate of points
    int y[] = { 3, 2 };
    int K = 1;
  
    int n = sizeof(x) / sizeof(x[0]);
  
    kClosestPoints(x, y, n, K);
  
    return 0;
}


Java
// Java implementation to find the K
// closest points to origin
// using Priority Queue
import java.util.*;
  
// Point class to store
// a point
class Pair implements Comparable
{
    int first, second;
    Pair(int a, int b)
    {
        first = a;
        second = b;
    }
      
    public int compareTo(Pair o)
    {
        int x1 = first * first;
        int y1 = second * second;
        int x2 = o.first * o.first;
        int y2 = o.second * o.second;
        return (x1 + y1) - (x2 + y2);
    }
}
  
class GFG{
      
// Function to find the K closest points
static void kClosestPoints(int x[], int y[], 
                           int n, int k)
{
    // Create a priority queue
    PriorityQueue pq = new PriorityQueue<>();
  
    // Pushing all the points
    // in the queue
    for(int i = 0; i < n; i++)
    {
        pq.add(new Pair(x[i], y[i]));
    }
  
    // Print the first K elements
    // of the queue
    for(int i = 0; i < k; i++) 
    {
  
        // Remove the top of the queue
        // and store in a temporary pair
        Pair p = pq.poll();
  
        // Print the first (x)
        // and second (y) of pair
        System.out.println(p.first + 
                     " " + p.second);
    }
}
  
// Driver code
public static void main(String[] args)
{
      
    // x coordinate of points
    int x[] = { 1, -2 };
  
    // y coordinate of points
    int y[] = { 3, 2 };
    int K = 1;
  
    int n = x.length;
  
    kClosestPoints(x, y, n, K);
}
}
  
// This code is contributed by jrishabh99


输出:
-2 2

时间复杂度: O(n + K * log(n))