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

📅  最后修改于: 2021-04-26 07:29:31             🧑  作者: Mango

鉴于城市的数n为n-1,其中站所在城市从0,任务是找到任何城市和它最近的车站之间的最大距离。请注意,带有车站的城市可以以任何顺序给出。

例子:

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


输出:
Max Distance: 2

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