|
| 1 | +# 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance - Best Practices of LeetCode Solutions |
| 2 | +LeetCode link: [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), difficulty: **Medium**. |
| 3 | + |
| 4 | +## LeetCode description of "1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance" |
| 5 | +There are `n` cities numbered from `0` to `n-1`. Given the array edges where `edges[i] = [from_i, to_i, weight_i]` represents a bidirectional and weighted edge between cities `from_i` and `to_i`, and given the integer `distanceThreshold`. |
| 6 | + |
| 7 | +Return the city with the smallest number of cities that are reachable through some path and whose distance is **at most** `distanceThreshold`, If there are multiple such cities, return the city with the greatest number. |
| 8 | + |
| 9 | +Notice that the distance of a path connecting cities _**i**_ and _**j**_ is equal to the sum of the edges' weights along that path. |
| 10 | + |
| 11 | +### [Example 1] |
| 12 | + |
| 13 | + |
| 14 | +**Input**: `n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4` |
| 15 | + |
| 16 | +**Output**: `3` |
| 17 | + |
| 18 | +**Explanation**: |
| 19 | +``` |
| 20 | +The figure above describes the graph. |
| 21 | +The neighboring cities at a distanceThreshold = 4 for each city are: |
| 22 | +City 0 -> [City 1, City 2] |
| 23 | +City 1 -> [City 0, City 2, City 3] |
| 24 | +City 2 -> [City 0, City 1, City 3] |
| 25 | +City 3 -> [City 1, City 2] |
| 26 | +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. |
| 27 | +``` |
| 28 | + |
| 29 | +### [Example 2] |
| 30 | + |
| 31 | + |
| 32 | +**Input**: `n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2` |
| 33 | + |
| 34 | +**Output**: `0` |
| 35 | + |
| 36 | +**Explanation**: |
| 37 | +``` |
| 38 | +The figure above describes the graph. |
| 39 | +The neighboring cities at a distanceThreshold = 2 for each city are: |
| 40 | +City 0 -> [City 1] |
| 41 | +City 1 -> [City 0, City 4] |
| 42 | +City 2 -> [City 3, City 4] |
| 43 | +City 3 -> [City 2, City 4] |
| 44 | +City 4 -> [City 1, City 2, City 3] |
| 45 | +The city 0 has 1 neighboring city at a distanceThreshold = 2. |
| 46 | +``` |
| 47 | + |
| 48 | +### [Constraints] |
| 49 | +- `2 <= n <= 100` |
| 50 | +- `1 <= edges.length <= n * (n - 1) / 2` |
| 51 | +- `edges[i].length == 3` |
| 52 | +- `0 <= from_i < to_i < n` |
| 53 | +- `1 <= weight_i, distanceThreshold <= 10^4` |
| 54 | +- All pairs `(from_i, to_i)` are distinct. |
| 55 | + |
| 56 | +### [Hints] |
| 57 | +<details> |
| 58 | + <summary>Hint 1</summary> |
| 59 | + Use Floyd-Warshall's algorithm to compute any-point to any-point distances. (Or can also do Dijkstra from every node due to the weights are non-negative). |
| 60 | +</details> |
| 61 | + |
| 62 | +<details> |
| 63 | + <summary>Hint 2</summary> |
| 64 | + For each city calculate the number of reachable cities within the threshold, then search for the optimal city. |
| 65 | +</details> |
| 66 | + |
| 67 | +## Intuition |
| 68 | +Just like the `Hints` says, you can use **Floyd-Warshall algorithm** to compute any-point to any-point shortest distances. |
| 69 | + |
| 70 | +Or you can also do **Dijkstra algorithm** from every node due to the weights are non-negative. |
| 71 | + |
| 72 | +## Complexity |
| 73 | +* Time: `O(N^3)`. |
| 74 | +* Space: `O(N^2)`. |
| 75 | + |
| 76 | +## Python |
| 77 | +```python |
| 78 | +class Solution: |
| 79 | + def findTheCity(self, n: int, edges: List[List[int]], distance_threshold: int) -> int: |
| 80 | + dp = [] |
| 81 | + |
| 82 | + for i in range(n): |
| 83 | + dp.append([float('inf')] * n) |
| 84 | + dp[i][i] = 0 |
| 85 | + |
| 86 | + for i, j, weight in edges: |
| 87 | + dp[i][j] = weight |
| 88 | + dp[j][i] = weight |
| 89 | + |
| 90 | + for k in range(n): |
| 91 | + for i in range(n): |
| 92 | + for j in range(n): |
| 93 | + dp[i][j] = min( |
| 94 | + dp[i][j], |
| 95 | + dp[i][k] + dp[k][j], |
| 96 | + ) |
| 97 | + |
| 98 | + result = -1 |
| 99 | + min_count = float('inf') |
| 100 | + |
| 101 | + for i, row in enumerate(dp): |
| 102 | + count = len([distance for distance in row if distance <= distance_threshold]) |
| 103 | + |
| 104 | + if count <= min_count: |
| 105 | + min_count = count |
| 106 | + result = i |
| 107 | + |
| 108 | + return result |
| 109 | +``` |
| 110 | + |
| 111 | +## JavaScript |
| 112 | +```javascript |
| 113 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 114 | +``` |
| 115 | + |
| 116 | +## Java |
| 117 | +```java |
| 118 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 119 | +``` |
| 120 | + |
| 121 | +## C++ |
| 122 | +```cpp |
| 123 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 124 | +``` |
| 125 | + |
| 126 | +## C# |
| 127 | +```c# |
| 128 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 129 | +``` |
| 130 | + |
| 131 | +## Go |
| 132 | +```go |
| 133 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 134 | +``` |
| 135 | + |
| 136 | +## Ruby |
| 137 | +```ruby |
| 138 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 139 | +``` |
| 140 | + |
| 141 | +## C, Kotlin, Swift, Rust or other languages |
| 142 | +``` |
| 143 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 144 | +``` |
0 commit comments