Search
⌘K
Graphs

Find City with Fewest Reachable

medium

DESCRIPTION (inspired by Leetcode.com)

A regional planning committee wants to identify the most "isolated" city in a road network. Given n cities connected by bidirectional roads with varying distances, find the city that can reach the fewest other cities within a given distance threshold.

If multiple cities have the same minimum count, return the city with the highest number.

Note: The distance between two cities is the shortest path distance considering all possible routes.

Example 1:

01233141Threshold: 4Answer: 32 reachable

Input:

n = 4
edges = [[0,1,3], [1,2,1], [1,3,4], [2,3,1]]
distanceThreshold = 4

Output:

3

Explanation: After computing shortest paths, city 3 can reach cities 1 (distance 2) and 2 (distance 1) within the threshold — 2 reachable. City 0 can also reach 2 cities, but city 3 wins the tie by having the highest number.

Example 2:

01234231118Threshold: 2Answer: 01 reachable

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

Explanation: With threshold 2, city 0 can only reach city 1 (distance 2). Other cities can reach more neighbors. City 0 has the fewest reachable.

Explanation

Understanding the Problem

Why This Differs from Previous Problems

The Floyd-Warshall Approach

The Algorithm Step by Step

Walkthrough

When to Use Floyd-Warshall vs. Running Dijkstra n Times

Solution

Purchase Premium to Keep Reading

Unlock this article and so much more with Hello Interview Premium