📌  相关文章
📜  最小距离,即对于每个客户,在给定距离内至少有一个供应商

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

最小距离,即对于每个客户,在给定距离内至少有一个供应商

给定直线上的NM个点,分别表示客户和供应商的位置。每个供应商都向所有客户提供服务,这些客户与供应商的距离不超过R。任务是找到最小R ,使得对于每个客户,在不超过R的距离处至少有一个供应商。

例子:

方法:这个问题可以通过使用双指针方法来解决。请按照以下步骤解决给定的问题。

  • 取两个指针, i代表客户数组, j代表供应商数组。
  • 开始移动i指针,对于每个客户,customer[i] > vendor[j]时移动j索引。
  • 现在当vendor[j] >= customer[i]时,
    • 所以检查它们之间的正确距离,即vendor[j] – customer[i ] if j < m
    • 如果j > 0 ,则检查左侧距离,即customer[i] – vendor[j – 1]
    • 找出这两个中的最小值,即customer[i]可以覆盖的最短范围;通过比较这两个相邻供应商的距离来实现。
    • 然后尽可能地最大化这个属性来得到答案。
  • 最后打印找到的答案。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find the minimal R.
int findMinDist(int customer[], int vendor[],
                int N, int M)
{
    // Variable to keep track of the minimal r
    int minR = 0;
 
    int i = 0, j = 0;
 
    // Two pointer approach
    while (i < N) {
        if (j < M and vendor[j] < customer[i])
            j++;
        else {
            int left_d = INT_MAX;
            int right_d = INT_MAX;
 
            // Find the distance of customer
            // from left vendor.
            if (j > 0)
                left_d = customer[i]
                         - vendor[j - 1];
 
            // Find the distance of customer
            // from right vendor.
            if (j < M)
                right_d = vendor[j]
                          - customer[i];
 
            // Find the minimum of
            // left_d and right_d.
            int mn_d = min(left_d, right_d);
 
            // Maximize the minimum distance.
            minR = max(minR, mn_d);
 
            // Go to the next customer.
            i++;
        }
    }
    return minR;
}
 
// Driver code
int main()
{
    int customer[] = { -2, 2, 4 };
    int vendor[] = { -3, 0 };
 
    int N = sizeof(customer)
            / sizeof(customer[0]);
    int M = sizeof(vendor)
            / sizeof(vendor[0]);
 
    // Function Call
    cout << findMinDist(customer, vendor,
                        N, M);
 
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
class GFG
{
  static int INT_MAX = 2147483647;
 
  // Function to find the minimal R.
  static int findMinDist(int customer[], int vendor[],
                         int N, int M)
  {
 
    // Variable to keep track of the minimal r
    int minR = 0;
 
    int i = 0, j = 0;
 
    // Two pointer approach
    while (i < N) {
      if (j < M && vendor[j] < customer[i])
        j++;
      else {
        int left_d = INT_MAX;
        int right_d = INT_MAX;
 
        // Find the distance of customer
        // from left vendor.
        if (j > 0)
          left_d = customer[i] - vendor[j - 1];
 
        // Find the distance of customer
        // from right vendor.
        if (j < M)
          right_d = vendor[j] - customer[i];
 
        // Find the minimum of
        // left_d and right_d.
        int mn_d = Math.min(left_d, right_d);
 
        // Maximize the minimum distance.
        minR =Math.max(minR, mn_d);
 
        // Go to the next customer.
        i++;
      }
    }
    return minR;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int customer[] = { -2, 2, 4 };
    int vendor[] = { -3, 0 };
 
    int N = customer.length;
    int M = vendor.length;
 
    // Function Call
 
    System.out.println(
      findMinDist(customer, vendor, N, M));
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for above approach
INT_MAX = 2147483647
 
# Function to find the minimal R.
def findMinDist(customer, vendor, N, M):
 
    # Variable to keep track of the minimal r
    minR = 0
 
    i = 0
    j = 0
 
    # Two pointer approach
    while (i < N):
        if (j < M and vendor[j] < customer[i]):
            j += 1
        else:
            left_d = 10 ** 9
            right_d = 10 ** 9
 
            # Find the distance of customer
            # from left vendor.
            if (j > 0):
                left_d = customer[i] - vendor[j - 1]
 
            # Find the distance of customer
            # from right vendor.
            if (j < M):
                right_d = vendor[j] - customer[i]
 
            # Find the minimum of
            # left_d and right_d.
            mn_d = min(left_d, right_d)
 
            # Maximize the minimum distance.
            minR = max(minR, mn_d)
 
            # Go to the next customer.
            i += 1
    return minR
 
# Driver code
customer = [-2, 2, 4]
vendor = [-3, 0]
 
N = len(customer)
M = len(vendor)
 
# Function Call
print(findMinDist(customer, vendor, N, M))
 
# This code is contributed by Saurabh Jaiswal


Javascript


C#
// C# program to implement
// the above approach
using System;
class GFG
{
  static int INT_MAX = 2147483647;
 
  // Function to find the minimal R.
  static int findMinDist(int []customer, int []vendor,
                         int N, int M)
  {
 
    // Variable to keep track of the minimal r
    int minR = 0;
 
    int i = 0, j = 0;
 
    // Two pointer approach
    while (i < N) {
      if (j < M && vendor[j] < customer[i])
        j++;
      else {
        int left_d = INT_MAX;
        int right_d = INT_MAX;
 
        // Find the distance of customer
        // from left vendor.
        if (j > 0)
          left_d = customer[i] - vendor[j - 1];
 
        // Find the distance of customer
        // from right vendor.
        if (j < M)
          right_d = vendor[j] - customer[i];
 
        // Find the minimum of
        // left_d and right_d.
        int mn_d = Math.Min(left_d, right_d);
 
        // Maximize the minimum distance.
        minR =Math.Max(minR, mn_d);
 
        // Go to the next customer.
        i++;
      }
    }
    return minR;
  }
 
  // Driver code
  public static void Main()
  {
    int []customer = { -2, 2, 4 };
    int []vendor = { -3, 0 };
 
    int N = customer.Length;
    int M = vendor.Length;
 
    // Function Call
 
    Console.WriteLine(
      findMinDist(customer, vendor, N, M));
  }
}
 
// This code is contributed by Samim Hossain Mondal.



输出
4

时间复杂度: O(N + M)
辅助空间: O(1)