📜  查找任何城市和车站之间的最大距离

📅  最后修改于: 2021-10-27 16:55:19             🧑  作者: Mango

给定从 0 到 n-1 编号的城市数量n以及车站所在的城市,任务是找到任何城市与其最近车站之间的最大距离。请注意,可以按任何顺序给出带有车站的城市。
例子:

Input: numOfCities = 6, stations = [1, 4]
Output: 1

Input: numOfCities = 6, stations = [3]
Output: 3

Input: numOfCities = 6, stations = [3, 1]
Output: 2
  1. 下图表示第一个示例,其中包含 6 个城市以及车站以绿色突出显示的城市。在这种情况下,距离最近站点最远的城市分别为 0、2、3 和 5,距离为 1。因此,最大距离为 1。
  2. 在第二个示例中,距离最近的车站最远的城市是 0,距离为 3。因此,最大距离为 3。
  3. 在第三个示例中,距离最近的车站最远的城市是 5,距离为 2。因此,最大距离为 2。

处理方法:这个问题有三种可能的情况:

  1. 当最远的城市在两个车站之间时。
  2. 当最远的城市在第一站的左侧。
  3. 当最远的城市在最后一站的右侧时。

下面是解决上述问题的算法:

  • False初始化一个大小为n (城市数量)的布尔数组。然后将带有站点的城市的值标记为True
  • 用 0 初始化变量dist。用等于第一个带有车站的城市的值初始化另一个变量maxDist (用于情况 2)。
  • 开始一个一个地循环遍历所有城市。
  • 如果当前城市有车站,则将( dist +1)//2和maxDist的最大值赋值maxDist (用于情况1)。此外,将 0 分配给dist
  • 否则,增加dist
  • 最后,返回distmaxDist的最大值(用于情况 3)。

下面是上述方法的实现:

C++
// C++ program to calculate the maximum 
// distance between any city
// and its nearest station
#include
  
using namespace std;
  
// Function to calculate the maximum 
// distance between any city and its nearest station
int findMaxDistance(int numOfCities,int station[],int n)
{ 
    // Initialize boolean list
    bool hasStation[numOfCities + 1] = {false};
      
    // Assign True to cities containing station
    for (int city = 0; city < n; city++)
    {
        hasStation[station[city]] = true;
    }
          
    int dist = 0;
    int maxDist = INT_MAX;
  
    for(int i = 0; i < n; i++)
    {
        maxDist = min(station[i],maxDist);
    }
  
    for (int city = 0; city < numOfCities; city++)
    {
        if (hasStation[city] == true)
        {
            maxDist = max((dist + 1) / 2, maxDist);
            dist = 0;
        }
        else
            dist += 1;
    }
    return max(maxDist, dist);
} 
  
//Driver code
int main()
{ 
    int numOfCities = 6;
    int station[] = {3, 1};
    int n = sizeof(station)/sizeof(station[0]);
  
    cout << "Max Distance:" << findMaxDistance(numOfCities,
                                                station, n);
}
  
//This code is contributed by Mohit Kumar 29


Java
// Java program to calculate the maximum 
// distance between any city
// and its nearest station
import java.util.*;
  
class GFG
{
  
// Function to calculate the maximum 
// distance between any city and its nearest station
static int findMaxDistance(int numOfCities,
                            int station[],int n)
{ 
    // Initialize boolean list
    boolean hasStation[] = new boolean[numOfCities + 1];
      
    // Assign True to cities containing station
    for (int city = 0; city < n; city++)
    {
    hasStation[station[city]] = true;
    }
          
    int dist = 0;
    int maxDist = Integer.MAX_VALUE;
  
    for(int i = 0; i < n; i++)
    {
        maxDist = Math.min(station[i],maxDist);
    }
  
    for (int city = 0; city < numOfCities; city++)
    {
        if (hasStation[city] == true)
        {
            maxDist = Math.max((dist + 1) / 2, maxDist);
            dist = 0;
        }
        else
            dist += 1;
    }
    return Math.max(maxDist, dist);
} 
  
//Driver code
public static void main(String args[])
{ 
    int numOfCities = 6;
    int station[] = {3, 1};
    int n = station.length;
  
    System.out.println("Max Distance:"+
        findMaxDistance(numOfCities,station, n));
}
}
  
// This code is contributed by
// Surendra_Gnagwar


Python
# Python3 code to calculate the maximum 
# distance between any city and its nearest station
  
# Function to calculate the maximum 
# distance between any city and its nearest station
def findMaxDistance(numOfCities, station):
      
    # Initialize boolean list
    hasStation = [False] * numOfCities
    # Assign True to cities containing station
    for city in station:
        hasStation[city] = True
          
    dist, maxDist = 0, min(station)
  
    for city in range(numOfCities):
        if hasStation[city] == True:
            maxDist = max((dist + 1) // 2, maxDist)
            dist = 0
              
        else:
            dist += 1
              
    return max(maxDist, dist)
      
numOfCities = 6
station = [3, 1]
print("Max Distance:", findMaxDistance(numOfCities, station))


C#
// C# program to calculate the maximum 
// distance between any city 
// and its nearest station 
using System;
  
class GFG 
{ 
  
// Function to calculate the maximum 
// distance between any city and its nearest station 
static int findMaxDistance(int numOfCities, 
                            int []station,int n) 
{ 
    // Initialize boolean list 
    bool []hasStation = new bool[numOfCities + 1]; 
      
    // Assign True to cities containing station 
    for (int city = 0; city < n; city++) 
    { 
        hasStation[station[city]] = true; 
    } 
          
    int dist = 0; 
    int maxDist = int.MaxValue; 
  
    for(int i = 0; i < n; i++) 
    { 
        maxDist = Math.Min(station[i],maxDist); 
    } 
  
    for (int city = 0; city < numOfCities; city++) 
    { 
        if (hasStation[city] == true) 
        { 
            maxDist = Math.Max((dist + 1) / 2, maxDist); 
            dist = 0; 
        } 
        else
            dist += 1; 
    } 
    return Math.Max(maxDist, dist); 
} 
  
// Driver code 
public static void Main(String []args) 
{ 
    int numOfCities = 6; 
    int []station = {3, 1}; 
    int n = station.Length; 
  
    Console.WriteLine("Max Distance:"+ 
        findMaxDistance(numOfCities,station, n)); 
} 
} 
  
// This code has been contributed by 29AjayKumar


PHP


Javascript


输出:
Max Distance: 2

时间复杂度: O(n)
空间复杂度: O(n)