Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1334: Find the City With the Smallest Number of Neighbors at a Threshold Distance

In this guide, we solve Leetcode #1334 Find the City With the Smallest Number of Neighbors at a Threshold Distance in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Graph, Dynamic Programming, Shortest Path

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

Input: n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 Output: 3 Explanation: The figure above describes the graph.  The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -> [City 1, City 2]  City 1 -> [City 0, City 2, City 3]  City 2 -> [City 0, City 1, City 3]  City 3 -> [City 1, City 2]  Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.

Python Solution

class Solution: def findTheCity( self, n: int, edges: List[List[int]], distanceThreshold: int ) -> int: def dijkstra(u: int) -> int: dist = [inf] * n dist[u] = 0 vis = [False] * n for _ in range(n): k = -1 for j in range(n): if not vis[j] and (k == -1 or dist[k] > dist[j]): k = j vis[k] = True for j in range(n): # dist[j] = min(dist[j], dist[k] + g[k][j]) if dist[k] + g[k][j] < dist[j]: dist[j] = dist[k] + g[k][j] return sum(d <= distanceThreshold for d in dist) g = [[inf] * n for _ in range(n)] for f, t, w in edges: g[f][t] = g[t][f] = w ans, cnt = n, inf for i in range(n - 1, -1, -1): if (t := dijkstra(i)) < cnt: cnt, ans = t, i return ans

Complexity

The time complexity is O(n·m) (typical). The space complexity is O(n·m) or optimized.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy