1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 70% Topics: Dynamic Programming, Graph, Shortest Path
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n^3)
# Space: O(n^2)
class Solution(object):
def findTheCity(self, n, edges, distanceThreshold):
"""
:type n: int
:type edges: List[List[int]]
:type distanceThreshold: int
:rtype: int
"""
dist = [[float("inf")]*n for _ in xrange(n)]
for i, j, w in edges:
dist[i][j] = dist[j][i] = w
for i in xrange(n):
dist[i][i] = 0
for k in xrange(n):
for i in xrange(n):
for j in xrange(n):
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])
result = {sum(d <= distanceThreshold for d in dist[i]): i for i in xrange(n)}
return result[min(result.iterkeys())]
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions