Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find maximum distance between any city and station in Python
Sometimes we need to find the maximum distance between any city and its nearest station. Given N cities numbered from 0 to N-1 and a list of cities with stations, we need to calculate the furthest any city is from a station.
So, if the input is like N = 6 and stations = [2, 4], then the output will be 2, because city 0 is distance 2 from the nearest station at city 2.
Algorithm Overview
To solve this problem, we will follow these steps ?
Create a boolean array to mark which cities have stations
Initialize distance counter and maximum distance found so far
Iterate through all cities, calculating distances between stations
For gaps between stations, the maximum distance is at the middle point
Handle the distance from the last station to the end
Implementation
Let us see the complete implementation to get better understanding ?
def get_max_dist(N, stations):
# Mark which cities have stations
station_present = [False] * N
for city in stations:
station_present[city] = True
# Initialize variables
dist = 0
maximum_dist = min(stations) # Distance from city 0 to first station
# Iterate through all cities
for city in range(N):
if station_present[city] == True:
# Found a station, calculate max distance in the gap
maximum_dist = max((dist + 1) // 2, maximum_dist)
dist = 0
else:
# No station, increment distance
dist += 1
# Handle distance from last station to end
return max(maximum_dist, dist)
# Test the function
N = 6
stations = [2, 4]
result = get_max_dist(N, stations)
print(f"Maximum distance: {result}")
Maximum distance: 2
How It Works
The algorithm works by identifying gaps between stations and finding the maximum distance any city can be from its nearest station:
Cities 0-1: Distance 2 and 1 from station at city 2
City 2: Has a station (distance 0)
City 3: Distance 1 from stations at cities 2 or 4
City 4: Has a station (distance 0)
City 5: Distance 1 from station at city 4
Another Example
Let's test with different station positions ?
def get_max_dist(N, stations):
station_present = [False] * N
for city in stations:
station_present[city] = True
dist = 0
maximum_dist = min(stations)
for city in range(N):
if station_present[city] == True:
maximum_dist = max((dist + 1) // 2, maximum_dist)
dist = 0
else:
dist += 1
return max(maximum_dist, dist)
# Test with stations at positions 1 and 5
N = 8
stations = [1, 5]
result = get_max_dist(N, stations)
print(f"N = {N}, stations = {stations}")
print(f"Maximum distance: {result}")
N = 8, stations = [1, 5] Maximum distance: 2
Conclusion
This algorithm efficiently finds the maximum distance between any city and its nearest station by tracking gaps between stations. The time complexity is O(N) and space complexity is O(N) for the boolean array.
