📌  相关文章
📜  找到距离原点最近的 K 个点

📅  最后修改于: 2021-10-23 08:17:16             🧑  作者: Mango

给定二维平面上的点列表和整数 K。任务是找到距离原点最近的 K 个点并打印它们。
注意:平面上两点之间的距离是欧氏距离。

例子:

Input : point = [[3, 3], [5, -1], [-2, 4]], K = 2
Output : [[3, 3], [-2, 4]]
Square of Distance of origin from this point is 
(3, 3) = 18
(5, -1) = 26
(-2, 4) = 20
So rhe closest two points are [3, 3], [-2, 4].

Input : point = [[1, 3], [-2, 2]], K  = 1
Output : [[-2, 2]]
Square of Distance of origin from this point is
(1, 3) = 10
(-2, 2) = 8 
So the closest point to origin is (-2, 2)

方法:想法是计算每个给定点距原点的欧几里德距离,并根据找到的欧几里德距离对数组进行排序。打印列表中前 k 个最近的点。

算法 :
考虑坐标分别为 (x1, y1) 和 (x2, y2) 的两个点。这两点之间的欧几里得距离将是:

√{(x2-x1)2 + (y2-y1)2}
  1. 使用欧几里得距离公式按距离对点进行排序。
  2. 从列表中选择前 K 个点
  3. 以任意顺序打印获得的点数。

下面是上述方法的实现:

C++
// C++ program for implementation of 
// above approach
#include
using namespace std;
 
// Function to print required answer
void pClosest(vector> pts, int k)
{
     
    // In multimap values gets
    // automatically sorted based on
    // their keys which is distance here
    multimap mp;
    for(int i = 0; i < pts.size(); i++)
    {
        int x = pts[i][0], y = pts[i][1];
        mp.insert({(x * x) + (y * y) , i});
    }
     
    for(auto it = mp.begin();
             it != mp.end() && k > 0;
             it++, k--)
        cout << "[" << pts[it->second][0] << ", "
             << pts[it->second][1] << "]" << "\n";
}
 
// Driver code
int main()
{
    vector> points = { { 3, 3 },
                                   { 5, -1 },
                                   { -2, 4 } };
     
    int K = 2;
     
    pClosest(points, K);
    return 0;
}
 
// This code is contributed by sarthak_eddy.


Java
// Java program for implementation of 
// above approach
import java.util.*;
 
class GFG{
     
// Function to print required answer
static void pClosest(int [][]pts, int k)
{
    int n = pts.length;
    int[] distance = new int[n];
    for(int i = 0; i < n; i++)
    {
        int x = pts[i][0], y = pts[i][1];
        distance[i] = (x * x) + (y * y);
    }
 
    Arrays.sort(distance);
     
    // Find the k-th distance
    int distk = distance[k - 1];
 
    // Print all distances which are
    // smaller than k-th distance
    for(int i = 0; i < n; i++)
    {
        int x = pts[i][0], y = pts[i][1];
        int dist = (x * x) + (y * y);
         
        if (dist <= distk)
            System.out.println("[" + x + ", " + y + "]");
    }
}
 
// Driver code
public static void main (String[] args)
{
    int points[][] = { { 3, 3 },
                       { 5, -1 },
                       { -2, 4 } };
 
    int K = 2;
     
    pClosest(points, K);
}
}
 
// This code is contributed by sarthak_eddy.


Python3
# Python3 program for implementation of
# above approach
 
# Function to return required answer
def pClosest(points, K):
 
    points.sort(key = lambda K: K[0]**2 + K[1]**2)
 
    return points[:K]
 
# Driver program
points = [[3, 3], [5, -1], [-2, 4]]
 
K = 2
 
print(pClosest(points, K))


C#
// C# program for implementation
// of above approach
using System;
class GFG{
     
// Function to print
// required answer
static void pClosest(int [,]pts,
                     int k)
{
  int n = pts.GetLength(0);
 
  int[] distance = new int[n];
   
  for(int i = 0; i < n; i++)
  {
    int x = pts[i, 0],
        y = pts[i, 1];
    distance[i] = (x * x) +
                  (y * y);
  }
 
  Array.Sort(distance);
 
  // Find the k-th distance
  int distk = distance[k - 1];
 
  // Print all distances which are
  // smaller than k-th distance
  for(int i = 0; i < n; i++)
  {
    int x = pts[i, 0],
        y = pts[i, 1];
    int dist = (x * x) +
               (y * y);
 
    if (dist <= distk)
      Console.WriteLine("[" + x +
                        ", " + y + "]");
  }
}
 
// Driver code
public static void Main (string[] args)
{
  int [,]points = {{3, 3},
                   {5, -1},
                   {-2, 4}};
  int K = 2;
  pClosest(points, K);
}
}
 
// This code is contributed by Chitranayal


Javascript


输出:
[[3, 3], [-2, 4]]

复杂度分析:

  • 时间复杂度: O(n log n)。
    找到每个点与原点的距离的时间复杂度为 O(n),对数组进行排序的时间复杂度为 O(n log n)
  • 空间复杂度: O(n)。
    因为我们正在制作一个数组来存储每个点与原点的距离。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程