Ctrl K

The Most Secluded Town

Medium
A region has n towns numbered 0 to n - 1, linked by two-way roads: roads[i] = [a, b, w] is a road of length w km between towns a and b. The distance between two towns is the length of the shortest route along the roads. For each town, count how many **other** towns are at distance at most max_distance from it. Return the town with the fewest such neighbours. If several towns tie, return the one with the largest number.
Example 1
n=3, roads=[[0, 1, 2], [1, 2, 2]], max_distance=3 → 2

From town 1 both other towns are within 3 km, while towns 0 and 2 each reach only town 1. Towns 0 and 2 tie, so the larger number, 2, wins.

Example 2
n=4, roads=[[0, 1, 1], [1, 2, 1], [2, 3, 5]], max_distance=2 → 3

Town 3 is reachable only over a road of length 5, so no town lies within 2 km of it.

Constraints
  • 2 ≤ n ≤ 100
  • 1 ≤ len(roads) ≤ n·(n − 1)/2
  • roads[i] = [a, b, w], 0 ≤ a, b < n, a ≠ b
  • 1 ≤ w ≤ 10⁴
  • 1 ≤ max_distance ≤ 10⁴
  • Any pair of towns has at most one road between them
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCheapest Trip With Few Layovers MediumRoutes That Keep Getting Closer MediumCounting the Fastest Routes MediumCheapest Hop Route MediumPhrase Rewrite Rules Hard
loading Python…
Input
n=3, roads=[[0, 1, 2], [1, 2, 2]], max_distance=3
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 62 tests on the server, hidden ones included.