You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance equal to distancei. The cities graph is not necessarily connected.
The score of a path between two cities is defined as the minimum distance of a road in this path.
Return the minimum possible score of a path between cities 1 and n.
Note:
1 and n multiple times along the path.1 and n.Example 1:
Input: n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]] Output: 5 Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5. It can be shown that no other path has less score.
Example 2:
Input: n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]] Output: 2 Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
Constraints:
2 <= n <= 1051 <= roads.length <= 105roads[i].length == 31 <= ai, bi <= nai != bi1 <= distancei <= 1041 and n.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you're trying to find the route with the smallest road distance between two cities. The brute force method checks every possible route between the cities, no matter how long or winding. We'll look at each path individually to find the shortest road.
Here's how the algorithm would work step-by-step:
def find_minimum_score_brute_force(number_of_cities, roads): # Represent the graph as an adjacency list. graph = [[] for _ in range(number_of_cities + 1)] for city_a, city_b, road_distance in roads: graph[city_a].append((city_b, road_distance)) graph[city_b].append((city_a, road_distance)) all_paths = [] def find_all_paths(start_city, end_city, current_path, visited): current_path.append(start_city) visited[start_city] = True if start_city == end_city: all_paths.append(current_path[:]) else: for neighbor, _ in graph[start_city]: if not visited[neighbor]: find_all_paths(neighbor, end_city, current_path, visited) current_path.pop() visited[start_city] = False # Find all paths from city 1 to city n. visited = [False] * (number_of_cities + 1) find_all_paths(1, number_of_cities, [], visited)
minimum_score = float('inf') # Iterate through all paths to find the minimum score. for path in all_paths: path_score = float('inf') for i in range(len(path) - 1): city_a = path[i] city_b = path[i+1] # Find the distance between two cities in the path. for neighbor, road_distance in graph[city_a]: if neighbor == city_b: path_score = min(path_score, road_distance) break minimum_score = min(minimum_score, path_score)
return minimum_scoreThe goal is to find the path between two cities that has the smallest, or 'minimum', road quality score. Instead of checking every single possible route, we'll use a method to efficiently explore the connections between cities, keeping track of the worst road we've seen so far.
Here's how the algorithm would work step-by-step:
def find_minimum_score(number_of_cities, roads):
graph = [[] for _ in range(number_of_cities + 1)]
for city_a, city_b, road_quality in roads:
graph[city_a].append((city_b, road_quality))
graph[city_b].append((city_a, road_quality))
minimum_road_quality = float('inf')
visited = [False] * (number_of_cities + 1)
cities_to_visit = [1]
visited[1] = True
# Use BFS to traverse the graph
while cities_to_visit:
current_city = cities_to_visit.pop(0)
# Explore all adjacent cities
for neighbor_city, road_quality in graph[current_city]:
minimum_road_quality = min(minimum_road_quality, road_quality)
# Avoid cycles by only visiting unvisited cities
if not visited[neighbor_city]:
cities_to_visit.append(neighbor_city)
visited[neighbor_city] = True
return minimum_road_quality| Case | How to Handle |
|---|---|
| Null or empty roads array | Return -1 immediately as no path information exists. |
| roads array containing an empty road (e.g., [[]]) | Treat such a road as invalid and skip it during processing; the problem statement should clarify if empty roads are possible, if not, throw an exception. |
| Only one city (n=1) | Return 0 immediately since there is no path between two distinct cities. |
| Disjoint graph: no path exists between city 1 and city n | The algorithm should return -1, or some other suitable indication that no path exists, after exploring all reachable nodes from city 1. |
| roads array containing invalid city numbers (e.g., city number > n) | Filter out or ignore any roads that refer to cities outside the valid range 1 to n, or throw an error. |
| roads array containing duplicate roads (same start, end, and distance) | The algorithm should treat these as the same road and not double-count them. |
| Very large distance values, potentially causing integer overflow during min calculations. | Use a 64-bit integer type or appropriate data type to avoid integer overflow during minimum distance calculations. |
| Roads with zero distance between cities. | Consider zero-distance roads during pathfinding, as they could be part of a path with the minimum overall score. |