Find the City With the Smallest Number of Neighbors at a Threshold Distance - Problem
Imagine you're a logistics manager tasked with finding the most isolated city in a transportation network. You have n cities numbered from 0 to n-1, connected by bidirectional roads with specific travel costs.
Given an array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional road between cities fromi and toi with travel cost weighti, and a distanceThreshold, you need to:
- Find the city that can reach the fewest number of other cities within the distance threshold
- If multiple cities have the same minimum count, return the one with the largest city number
The distance between cities is the sum of road weights along the shortest path connecting them.
Input & Output
example_1.py โ Basic Graph
$
Input:
n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
โบ
Output:
3
๐ก Note:
The shortest distances are: City 0: can reach [1,2,3] with distances [3,4,5] โ 2 cities within threshold. City 1: can reach [0,2,3] with distances [3,1,2] โ 3 cities within threshold. City 2: can reach [0,1,3] with distances [4,1,1] โ 3 cities within threshold. City 3: can reach [0,1,2] with distances [5,2,1] โ 2 cities within threshold. Cities 0 and 3 have minimum count (2), so we return 3 (higher number).
example_2.py โ Dense Graph
$
Input:
n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
โบ
Output:
0
๐ก Note:
With threshold 2, most cities can only reach 1-2 neighbors. City 0 can reach [1] with distance 2. City 1 can reach [0,4] with distances [2,2]. City 4 can reach [1,3] with distances [2,1]. After calculating all shortest paths, city 0 has the minimum reachable count.
example_3.py โ Single City
$
Input:
n = 1, edges = [], distanceThreshold = 1
โบ
Output:
0
๐ก Note:
With only one city, city 0 has 0 reachable neighbors, making it the answer by default.
Visualization
Tap to expand
Understanding the Visualization
1
Network Analysis
Map all cities and transportation routes with travel costs
2
Distance Calculation
Calculate shortest response times between all city pairs
3
Coverage Assessment
Count how many cities each potential location can serve within time threshold
4
Optimal Selection
Choose location serving fewest cities (prefer newer facilities on ties)
Key Takeaway
๐ฏ Key Insight: Pre-computing all shortest paths with Floyd-Warshall enables efficient comparison of every potential response center location
Time & Space Complexity
Time Complexity
O(n * (E + V) * log V)
Running Dijkstra n times, each taking O((E + V) * log V) time
โก Linearithmic
Space Complexity
O(V + E)
Space for adjacency list and priority queue in Dijkstra
โ Linear Space
Constraints
- 2 โค n โค 100
- 1 โค edges.length โค n * (n - 1) / 2
- edges[i].length == 3
- 0 โค fromi < toi < n
- 1 โค weighti, distanceThreshold โค 104
- All pairs (fromi, toi) are distinct
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code