给定二维平面上的点列表和整数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}
- 使用欧几里德距离公式按距离对点进行排序。
- 从列表中选择前K个点
- 以任何顺序打印获得的点。
下面是上述方法的实现:
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
输出:
[[3, 3], [-2, 4]]
复杂度分析:
- 时间复杂度: O(n log n)。
查找每个点到原点的距离的时间复杂度为O(n),对数组进行排序的时间复杂度为O(n log n) - 空间复杂度: O(1)。
由于不需要额外的空间。