给定K维空间中的N个点,其中和 .任务是确定点,使得从该点到N个点的曼哈顿距离之和最小。
曼哈顿距离是沿直角轴测量的两点之间的距离。在p1位于(x1, y1)且p2位于(x2, y2)的平面中,它是|x1 – x2| + |y1 – y2| .
例子:
Input: N = 3, K = 3, Points = {1, 1, 1}, {2, 2, 2}, {3, 3, 3}
Output: 2 2 2
Input: N = 4, K = 4, Points = {1, 6, 9, 6}, {5, 2, 5, 7}, {2, 0, 1, 5}, {4, 6, 3, 9}
Output: 2 2 3 6
办法:尽量减少曼哈顿距离,我们所要做的仅仅是那种在所有K尺寸和输出所述K尺寸的中间元素的点。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to print the required points which
// minimizes the sum of Manhattan distances
void minDistance(int n, int k, vector >& point)
{
// Sorting points in all k dimension
for (int i = 0; i < k; ++i)
sort(point[i].begin(), point[i].end());
// Output the required k points
for (int i = 0; i < k; ++i)
cout << point[i][(ceil((double)n / 2) - 1)] << " ";
}
// Driver code
int main()
{
int n = 4, k = 4;
vector > point = { { 1, 5, 2, 4 },
{ 6, 2, 0, 6 },
{ 9, 5, 1, 3 },
{ 6, 7, 5, 9 } };
// function call to print required points
minDistance(n, k, point);
return 0;
}
Java
// Java implementation of above approach
import java.util.Arrays;
class GFG
{
// Function to print the required
// points which minimizes the sum
// of Manhattan distances
static void minDistance(int n, int k,
int point[][])
{
// Sorting points in all k dimension
for (int i = 0; i < k; i++)
Arrays.sort(point[i]);
// Output the required k points
for (int i = 0; i < k; i++)
System.out.print(point[i][(int)
Math.ceil((double)(n / 2) - 1)] + " ");
}
// Driver code
public static void main(String[] args)
{
int n = 4;
int k = 4;
int point[][] = { { 1, 5, 2, 4 },
{ 6, 2, 0, 6 },
{ 9, 5, 1, 3 },
{ 6, 7, 5, 9 } };
// function call to print required points
minDistance(n, k, point);
}
}
// This code is contributed by Bilal
Python
# Python implementation of above approach
# Function to print the required points which
# minimizes the sum of Manhattan distances
def minDistance(n, k, point):
# Sorting points in all dimension
for i in range(k):
point[i].sort()
# Output the required k points
for i in range(k):
print(point[i][((n + 1) // 2) - 1], end =" ")
# Driver code
n = 4
k = 4
point = [[1, 5, 2, 4],
[6, 2, 0, 6],
[9, 5, 1, 3],
[6, 7, 5, 9]]
# function call to print required points
minDistance(n, k, point)
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to print the required
// points which minimizes the sum
// of Manhattan distances
static void minDistance(int n, int k,
int[][] point)
{
// Sorting points in all k dimension
for (int i = 0; i < k; i++)
Array.Sort(point[i]);
// Output the required k points
for (int i = 0; i < k; i++)
System.Console.Write(point[i][(int)
Math.Ceiling((double)(n / 2) - 1)] + " ");
}
// Driver code
public static void Main()
{
int n = 4;
int k = 4;
int[][] point = new int[][]{ new int[]{ 1, 5, 2, 4 },
new int[]{ 6, 2, 0, 6 },
new int[]{ 9, 5, 1, 3 },
new int[]{ 6, 7, 5, 9 } };
// function call to print required points
minDistance(n, k, point);
}
}
// This code is contributed by mits
PHP
Javascript
输出:
2 2 3 6
时间复杂度: O(k*nlog(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。