Find the City with the Smallest Number of Neighbors
Given n cities numbered from 0 to n - 1, undirected weighted edges, and distanceThreshold, return the city that can reach the fewest other cities using paths…
Given n cities numbered from 0 to n - 1, undirected weighted edges, and distanceThreshold, return the city that can reach the fewest other cities using paths whose total weight is at most distanceThreshold. Each edge [u, v, weight] connects u and v in both directions. Indirect paths are allowed. Count each reachable destination once and do not count the starting city itself. If several cities have the same minimum count, return the greatest city number. Cities not mentioned in any edge still exist and have zero reachable neighbors. Do not modify the caller's edges.
1 ≤ n ≤ 100 and 1 ≤ edges.length ≤ n × (n - 1) / 2. Together these conditions require at least two cities. Every edge is a unique triple [u, v, weight], with 0 ≤ u < v < n. Weights and distanceThreshold are integers from 1 through 10000. The visualizer accepts at most 6 cities for a readable cubic timeline. Run and Submit retain the full 100-city, 4950-edge domain.
Example 1: input n = 3, edges = [[0, 1, 5], [1, 2, 1]], distanceThreshold = 5; output 2. Cities 0 and 2 each reach one other city, while city 1 reaches two. The greatest tied city is 2; a distance exactly equal to 5 is included.
Example 2: input n = 4, edges = [[1, 2, 1], [1, 3, 1]], distanceThreshold = 2; output 0. City 0 exists even though no edge mentions it. Its zero reachable neighbors are fewer than the two neighbors reached by every other city. The greatest index wins only among cities with the minimum count, not among all cities.
Category: Graphs. Topic: graph. Difficulty: Hard. Patterns: graph-traversal.