Tensorum
LeetCode 1334 · Shortest Path / Floyd-Warshall

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

LeetCode 1334 · Medium · solution, intuition & step-by-step walkthrough

Floyd–Warshall, then count reachable cities.

MediumDynamic ProgrammingGraph TheoryShortest PathDijkstra's AlgorithmBellman–Ford Algorithm
751 msruntime · beats 8%
20 MBmemory · beats 80%
1approach
4accepted runs · last 30 Mar 2026

This is how I solved Find the City With the Smallest Number of Neighbors at a Threshold Distance (LeetCode 1334), a medium problem from the Shortest Path / Floyd-Warshall pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

You need shortest distances between every pair of cities, which Floyd–Warshall gives by trying every city as an intermediate stop. Then count how many cities each city reaches within the threshold.

Approach

  1. 1

    Initialise dist with edges and zeros on the diagonal.

  2. 2

    For each via: dist[i][j] = min(dist[i][j], dist[i][via] + dist[via][j]).

  3. 3

    Count cities within the threshold for each source.

  4. 4

    Ties go to the larger city number (<=).

How I solved it

Floyd–WarshallPythonO(n³) time
class Solution:
    def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
        #floyd marshal algo, multi source graph - via
        #dist[i][j]=min(dist[i][j],dist[i][via]+dist[via][j])
        dist = [[float("inf")]*n for _ in range(n)]
    
        for u,v,w in edges:
            dist[u][v]=w
            dist[v][u]=w

        for i in range(n):
            dist[i][i]=0
        
        for via in range(n):
            for i in range(n):
                for j in range(n):
                    if dist[i][via]==float("inf") or dist[via][j]==float("inf"):
                        continue
                    dist[i][j]=min(dist[i][j],dist[i][via]+dist[via][j])
        min_count=n
        src_city = None
        for src in range(n):
            count = 0
            for dest in range(n):
                if dist[src][dest]<=distanceThreshold:
                    count+=1
            if count<=min_count:
                min_count = count
                src_city=src
        return src_city
                
Loading the step-through…

Find the City With the Smallest Number of Neighbors at a Threshold Distance — FAQ

What is the intuition behind Find the City With the Smallest Number of Neighbors at a Threshold Distance?

You need shortest distances between every pair of cities, which Floyd–Warshall gives by trying every city as an intermediate stop. Then count how many cities each city reaches within the threshold.

How do you solve Find the City With the Smallest Number of Neighbors at a Threshold Distance (LeetCode 1334)?

1. Initialise dist with edges and zeros on the diagonal. 2. For each via: dist[i][j] = min(dist[i][j], dist[i][via] + dist[via][j]). 3. Count cities within the threshold for each source. 4. Ties go to the larger city number (<=).

What is the time and space complexity of Find the City With the Smallest Number of Neighbors at a Threshold Distance?

Time: O(n³). Space: O(n²). (Accepted approach: Floyd–Warshall.)

What are common mistakes in Find the City With the Smallest Number of Neighbors at a Threshold Distance?

Use ≤ so later (larger) cities win ties.