1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
dist = [[float("inf")]*n for _ in range(n)]
for i in range(n):
dist[i][i]= 0
for edge in edges:
i,j,w = edge
dist[i][j] = w
dist[j][i] = w
for k in range(n):
for i in range(n):
for j in range(n):
if i == j:
continue
dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j])
d = {i:sum([d <= distanceThreshold for d in dist[i]]) for i in range(n)}
return min(range(n), key = lambda x: (d[x],-x))
Previous222. Count Complete Tree NodesNext1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows
Last updated
Was this helpful?