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
Emergency Response Center SelectionCity 0City 1City 2City 33 min1 min4 min1 minCity 3 Coverage ZoneServes 2 cities (threshold: 4 min)Response AnalysisThreshold: 4 minutes๐Ÿฅ City 0: serves 2 cities๐Ÿฅ City 1: serves 3 cities๐Ÿฅ City 2: serves 3 cities๐Ÿฅ City 3: serves 2 cities โœ“Tie-breaker: Choose highestnumbered city (3 > 0)Floyd-Warshall computes all response times efficiently in O(nยณ)
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

n
2n
โšก Linearithmic
Space Complexity
O(V + E)

Space for adjacency list and priority queue in Dijkstra

n
2n
โœ“ 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
Asked in
Amazon 18 Google 15 Microsoft 12 Meta 8
42.0K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen