📌  相关文章
📜  使曼哈顿距离之和最小化的点数

📅  最后修改于: 2022-05-13 01:56:09.253000             🧑  作者: Mango

使曼哈顿距离之和最小化的点数

给定二维数组Points[][]K维空间中的N个点,其中1≤ N ≤ 10 51 ≤ K ≤ 5 。任务是确定点的数量(具有整数坐标),以使从这些点到N个点的曼哈顿距离之和最小化

例子:

方法:该方法基于排序为了最小化曼哈顿距离,只需对所有K维中的点进行排序,然后根据给定的点数进行处理。请按照以下步骤解决问题:

  • 案例-1 N奇数时:可根据以下观察解决
    • 这样的最佳点的数量将始终为1 ,因为在将它们在所有K维中排序后,将只有一个中点,曼哈顿距离的总和将达到最小值。
  • 案例 2N偶数时:以下观察有助于解决问题。
    • 在一维的基础上对点进行排序。
    • 对于每个维度,将有2 个中值索引,即(N/2)-1(N/2)
      • [ Points[(N/2) -1], Points[N/2] ]范围内的所有数字将充当中位数,其中曼哈顿距离的总和将达到最小值。
      • 因此,该维度的中位数坐标总数将为Points[(N/2)] – Points[(N/2)-1]+1
    • 同理,根据该维度对数字进行排序后,求出所有维度的中值坐标的个数,它们的乘积将构成K维空间中此类最优点的总数。

下面是上述方法的实现:

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)