Find the City With the Smallest Number of Neighbors at a Threshold Distance - Problem

There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.

Return the city with the smallest number of cities that are reachable through some path and whose distance is at most distanceThreshold. If there are multiple such cities, return the city with the greatest number.

Notice that the distance of a path connecting cities i and j is equal to the sum of the edges' weights along that path.

Input & Output

Example 1 — Basic Linear Graph
$ Input: n = 4, edges = [[0,1,3],[1,2,2],[2,3,1]], distanceThreshold = 4
Output: 0
💡 Note: City 0 can reach city 1 (distance 3). City 1 can reach cities 0,2,3 (distances 3,2,3). City 2 can reach cities 1,3 (distances 2,1). City 3 can reach cities 1,2 (distances 3,1). City 0 has the fewest reachable cities (1), so return 0.
Example 2 — Disconnected Components
$ Input: n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2]], distanceThreshold = 2
Output: 3
💡 Note: Within threshold 2: City 0 can reach city 1 (distance 2). City 1 can reach cities 0,4 (distances 2,2). City 2 has no reachable neighbors. City 3 has no reachable neighbors. City 4 can reach city 1 (distance 2). Cities 2 and 3 both have 0 reachable neighbors, so return the larger index 3.
Example 3 — Single City
$ Input: n = 1, edges = [], distanceThreshold = 1
Output: 0
💡 Note: Only one city exists, so it has 0 reachable neighbors and is the answer by default.

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.

Visualization

Tap to expand
Find City With Smallest Neighbors at Threshold Distance INPUT Graph with 4 cities 0 1 2 3 3 2 1 n = 4 edges = [ [0,1,3], [1,2,2], [2,3,1] ] distanceThreshold = 4 Edge weights shown in red FLOYD-WARSHALL STEPS 1 Initialize dist matrix Set dist[i][j] = INF, dist[i][i] = 0 2 Fill edge weights dist[u][v] = weight (both dirs) 3 Run Floyd-Warshall For k,i,j: min(d[i][j],d[i][k]+d[k][j]) 4 Count reachable cities Within threshold for each city Final Distance Matrix: 0 1 2 3 0: 0 3 5 6 cnt=1 1: 3 0 2 3 cnt=3 2: 5 2 0 1 cnt=2 3: 6 3 1 0 cnt=2 Green: within threshold (4) Red: exceeds threshold cnt = reachable neighbors count FINAL RESULT Reachable cities within threshold 4: City 0 Reaches: 1 city (only city 1) City 1 Reaches: 3 cities (0, 2, 3) City 2 Reaches: 2 cities (1, 3) City 3 Reaches: 2 cities (1, 2) Cities 2 and 3 both have min count (2) City 3 has greater index OUTPUT 3 OK - City 3 is the answer! Smallest neighbors, greatest index Key Insight: Floyd-Warshall computes shortest paths between ALL pairs of cities in O(n^3) time. After getting distances, count neighbors within threshold for each city. Return the city with minimum count (greatest index on tie). Time: O(n^3), Space: O(n^2) for distance matrix. TutorialsPoint - Find the City With the Smallest Number of Neighbors at a Threshold Distance | Floyd-Warshall Approach
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
18.5K Views
Medium Frequency
~25 min Avg. Time
892 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