Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita07268b

Browse files
committed
1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md Added Python solution Floyd-Warshall's algorithm.
1 parentcba9592 commita07268b

6 files changed

+297
-7
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,6 @@ You can skip the more difficult problems and do them later.
117117
-[1514. Path with Maximum Probability](en/1001-2000/1514-path-with-maximum-probability.md) was solved in_Python_ and 2 ways.
118118
-[743. Network Delay Time](en/1-1000/743-network-delay-time.md) was solved in_Python_ and 2 ways.
119119
-[787. Cheapest Flights Within K Stops](en/1-1000/787-cheapest-flights-within-k-stops.md) was solved in_Python_.
120+
-[1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance](en/1001-2000/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md) was solved in_Python_.
120121

121122
More LeetCode problems will be added soon.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
![](../../images/examples/1334_1.png)
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+
![](../../images/examples/1334_2.png)
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+
classSolution:
79+
deffindTheCity(self,n:int,edges: List[List[int]],distance_threshold:int) ->int:
80+
dp= []
81+
82+
for iinrange(n):
83+
dp.append([float('inf')]* n)
84+
dp[i][i]=0
85+
86+
for i, j, weightin edges:
87+
dp[i][j]= weight
88+
dp[j][i]= weight
89+
90+
for kinrange(n):
91+
for iinrange(n):
92+
for jinrange(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, rowinenumerate(dp):
102+
count=len([distancefor distancein rowif 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+
```

‎en/3001-4000/unorganized.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ The improved way with a queue is commonly more efficient. Relaxing **All Edges**
5959
* The time complexity of`Floyd–Warshall algorithm` is`V * V * V`. For a dense graph,`Floyd–Warshall algorithm` is still faster.
6060
*`A* algorithm` use a`priority queue`,`pop()` to get the vertex closest to the destination vertex. We need to choose**proper math formula** to determine which one is the closest. We to the very near place of destination vertex, we can use some special method to make it can handle the last part.
6161

62-
|Algorithm name|Key implementation methods|
63-
|Prim's algorithm||
64-
|Kruskal's algorithm|Union-Find|
65-
|Dijkstra's algorithm||
66-
|Bellman-Ford algorithm||
62+
|Algorithm name|Focus|Key implementation methods|
63+
|Prim's algorithm|Vertices||
64+
|Kruskal's algorithm|Edges|Union-Find|
65+
|Dijkstra's algorithm|Vertices||
66+
|Bellman-Ford algorithm|Edges(Vertices+Edges for SPFA)||
6767

6868
##Others
6969
* Find all the prime numbers within 1000000.
@@ -172,8 +172,8 @@ The improved way with a queue is commonly more efficient. Relaxing **All Edges**
172172

173173
###Graph
174174
- 417https://leetcode.com/problems/pacific-atlantic-water-flow/
175-
-
176-
175+
-399https://leetcode.com/problems/evaluate-division/ union-find
176+
- 1976https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/ both use Dijkstra or Bellman-Ford can solve it.
177177

178178
###Failed in 2 rounds
179179
- 222https://leetcode.cn/problems/count-complete-tree-nodes/
@@ -190,5 +190,6 @@ The improved way with a queue is commonly more efficient. Relaxing **All Edges**
190190
- 417https://leetcode.com/problems/pacific-atlantic-water-flow/
191191
- 1584-min-cost-to-connect-all-points-2.md
192192
- 1514-path-with-maximum-probability.md
193+
- 1334https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance
193194

194195
2005-02-09 day 2

‎images/examples/1334_1.png

82.1 KB
Loading

‎images/examples/1334_2.png

121 KB
Loading
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#1334. 阈值距离内邻居最少的城市 - 力扣题解最佳实践
2+
力扣链接:[1334. 阈值距离内邻居最少的城市](https://leetcode.cn/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance) ,难度:**中等**
3+
4+
##力扣“1334. 阈值距离内邻居最少的城市”问题描述
5+
`n` 个城市,按从`0``n-1` 编号。给你一个边数组`edges`,其中`edges[i] = [fromi, toi, weighti]` 代表`from_i``to_i` 两个城市之间的双向加权边,距离阈值是一个整数`distanceThreshold`
6+
7+
返回在路径距离限制为`distanceThreshold` 以内可到达城市最少的城市。如果有多个这样的城市,则返回编号**最大**的城市。
8+
9+
注意,连接城市_**i**__**j**_ 的路径的距离等于沿该路径的所有边的权重之和。
10+
11+
###[示例 1]
12+
![](../../images/examples/1334_1.png)
13+
14+
**输入**:`n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4`
15+
16+
**输出**:`3`
17+
18+
**解释**:
19+
```
20+
城市分布图如上。
21+
每个城市阈值距离 distanceThreshold = 4 内的邻居城市分别是:
22+
城市 0 -> [城市 1, 城市 2]
23+
城市 1 -> [城市 0, 城市 2, 城市 3]
24+
城市 2 -> [城市 0, 城市 1, 城市 3]
25+
城市 3 -> [城市 1, 城市 2]
26+
城市 0 和 3 在阈值距离 4 以内都有 2 个邻居城市,但是我们必须返回城市 3,因为它的编号最大。
27+
```
28+
29+
###[示例 2]
30+
![](../../images/examples/1334_2.png)
31+
32+
**输入**:`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+
**输出**:`0`
35+
36+
**解释**:
37+
```
38+
城市分布图如上。
39+
每个城市阈值距离 distanceThreshold = 2 内的邻居城市分别是:
40+
城市 0 -> [城市 1]
41+
城市 1 -> [城市 0, 城市 4]
42+
城市 2 -> [城市 3, 城市 4]
43+
城市 3 -> [城市 2, 城市 4]
44+
城市 4 -> [城市 1, 城市 2, 城市 3]
45+
城市 0 在阈值距离 2 以内只有 1 个邻居城市。
46+
```
47+
48+
###[约束]
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+
- 所有`(from_i, to_i)` 都是不同的。
55+
56+
###[提示]
57+
<details>
58+
<summary>提示 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>提示 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+
就像`提示`据说的,你可以使用**Floyd-Warshall 算法** 计算任意两点之间的最短距离。
69+
70+
或者,你可以多次调用**Dijkstra 算法**,每次调用时用不同的城市做为`起始城市`
71+
72+
##Complexity
73+
* Time:`O(N^3)`.
74+
* Space:`O(N^2)`.
75+
76+
##Python
77+
```python
78+
classSolution:
79+
deffindTheCity(self,n:int,edges: List[List[int]],distance_threshold:int) ->int:
80+
dp= []
81+
82+
for iinrange(n):
83+
dp.append([float('inf')]* n)
84+
dp[i][i]=0
85+
86+
for i, j, weightin edges:
87+
dp[i][j]= weight
88+
dp[j][i]= weight
89+
90+
for kinrange(n):
91+
for iinrange(n):
92+
for jinrange(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, rowinenumerate(dp):
102+
count=len([distancefor distancein rowif 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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp