City with the fewest reachable neighbors
Standard · Write code · about 15 min
There are n cities and two-way roads edges, where edges[i] = [a, b, length]. A city reaches another when the shortest road distance between them is at most distanceThreshold.
Return the city that reaches the fewest other cities. On a tie, return the largest city number.
Example 1
Input:
[4,[[0,1,3],[1,2,1],[1,3,4],[2,3,1]],4]
Output:
3
Cities 0 and 3 each reach 2 others within 4. City 3 is larger.
Example 2
Input:
[5,[[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]],2]
Output:
0
City 0 reaches only city 1.
Constraints
- 2 ≤ n ≤ 100
- 0 ≤ edges.length ≤ 1,000
- 1 ≤ length, distanceThreshold ≤ 10^4
Learn the idea first: Floyd-Warshall: all pairs, a visual lesson.
More Floyd-Warshall: all pairs problems
- Matrix after k = 1 Warm-up
- Implement Floyd-Warshall Standard
- Which loop goes outside? Standard