使曼哈顿距离之和最小化的点数
给定二维数组Points[][]中K维空间中的N个点,其中1≤ N ≤ 10 5和1 ≤ K ≤ 5 。任务是确定点的数量(具有整数坐标),以使从这些点到N个点的曼哈顿距离之和最小化。
Manhattan distance is the sum of distances between two points measured along axes at right angles. In a plane with p1 at (x1, y1) and p2 at (x2, y2), it is |x1 – x2| + |y1 – y2|.
例子:
Input: N = 3, K = 3,
Points = { {1, 1, 1},
{2, 2, 2},
{3, 3, 3} }
Output: 1
Explanation: From {2,2,2}, the sum of Manhattan distances to other 2 points is minimum
Input: N = 4, K = 4,
Points = { {1, 6, 9, 6},
{5, 2, 5, 7},
{2, 0, 1, 5},
{4, 6, 3, 9} }
Output: 90
方法:该方法基于排序。为了最小化曼哈顿距离,只需对所有K维中的点进行排序,然后根据给定的点数进行处理。请按照以下步骤解决问题:
- 案例-1 N为奇数时:可根据以下观察解决
- 这样的最佳点的数量将始终为1 ,因为在将它们在所有K维中排序后,将只有一个中点,曼哈顿距离的总和将达到最小值。
For example: Consider the expression |x-3| + |x-5| + |x-8|. This attains its minimum value only at a single point x = 5.
- 案例 2当N为偶数时:以下观察有助于解决问题。
- 在一维的基础上对点进行排序。
- 对于每个维度,将有2 个中值索引,即(N/2)-1和(N/2) 。
- [ Points[(N/2) -1], Points[N/2] ]范围内的所有数字将充当中位数,其中曼哈顿距离的总和将达到最小值。
- 因此,该维度的中位数坐标总数将为Points[(N/2)] – Points[(N/2)-1]+1 。
- 同理,根据该维度对数字进行排序后,求出所有维度的中值坐标的个数,它们的乘积将构成K维空间中此类最优点的总数。
For example: Consider the expression |x-3| + |x-5| + |x-8| + |x-10|. This attains its minimum value for all x in the range [5,8]. So there will be (8-5)+1 = 4 number of such x. Similarly, find for all the K dimensions.
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find the required number
// of points which minimizes the sum
// of Manhattan distances
int NumberOfPointsWithMinDistance(
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());
// When n is odd
if (n % 2 == 1) {
return 1;
}
// When n is even
int ans = 1;
for (int i = 0; i < k; ++i) {
int possible_points
= point[i][n / 2] -
point[i][(n / 2) - 1] + 1;
ans = ans * possible_points;
}
return ans;
}
int main()
{
int N = 4, K = 4;
vector >
Points = { { 1, 5, 2, 4 },
{ 6, 2, 0, 6 },
{ 9, 5, 1, 3 },
{ 6, 7, 5, 9 } };
cout << NumberOfPointsWithMinDistance(N, K, Points);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
// Function to find the required number
// of points which minimizes the sum
// of Manhattan distances
static int NumberOfPointsWithMinDistance(
int n, int k, int[][] point)
{
// Sorting points in all k dimension
for (int i = 0; i < k; ++i)
Arrays.sort(point[i]);
// When n is odd
if (n % 2 == 1) {
return 1;
}
// When n is even
int ans = 1;
for (int i = 0; i < k; ++i) {
int possible_points
= point[i][n / 2] -
point[i][(n / 2) - 1] + 1;
ans = ans * possible_points;
}
return ans;
}
public static void main(String[] args)
{
int N = 4, K = 4;
int[][]
Points = { { 1, 5, 2, 4 },
{ 6, 2, 0, 6 },
{ 9, 5, 1, 3 },
{ 6, 7, 5, 9 } };
System.out.print(NumberOfPointsWithMinDistance(N, K, Points));
}
}
// This code is contributed by gauravrajput1
Python3
# Python code for the above approach
# Function to find the required number
# of points which minimizes the sum
# of Manhattan distances
def NumberOfPointsWithMinDistance(n, k, points):
# Sorting points in all k dimension
for i in range(k):
points[i].sort()
# When n is odd
if (n % 2 == 1):
return 1
# When n is even
ans = 1
for i in range(k):
possible_points = points[i][(n // 2)] - points[i][(n // 2) - 1] + 1
ans = ans * possible_points
return ans
# Drive code
N = 4
K = 4
Points = [[1, 5, 2, 4], [6, 2, 0, 6], [9, 5, 1, 3], [6, 7, 5, 9]]
print(NumberOfPointsWithMinDistance(N, K, Points))
# This code is contributed by gfgking
C#
// C# implementation of above approach
using System;
using System.Linq;
public class GFG
{
// Function to find the required number
// of points which minimizes the sum
// of Manhattan distances
static int NumberOfPointsWithMinDistance(
int n, int k, int[,] point)
{
int []x = null;
// Sorting points in all k dimension
for (int i = 0; i < k; ++i)
{
x = GetRow(point, i);
Array.Sort(x);
for(int j = 0; j < x.GetLength(0); j++)
point[i,j] = x[j];
}
// When n is odd
if (n % 2 == 1) {
return 1;
}
// When n is even
int ans = 1;
for (int i = 0; i < k; ++i) {
int possible_points
= point[i,n / 2] -
point[i,(n / 2) - 1] + 1;
ans = ans * possible_points;
}
return ans;
}
public static int[] GetRow(int[,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int[rowLength];
for (var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
public static void Main(String[] args)
{
int N = 4, K = 4;
int[,]
Points = { { 1, 5, 2, 4 },
{ 6, 2, 0, 6 },
{ 9, 5, 1, 3 },
{ 6, 7, 5, 9 } };
Console.Write(NumberOfPointsWithMinDistance(N, K, Points));
}
}
// This code is contributed by 29AjayKumar
Javascript
90
时间复杂度: O(K * N * log(N))
辅助空间:O(1)