Skip to content

Commit b71622b

Browse files
author
lucifer
committed
feat: #1334
1 parent 71037a4 commit b71622b

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
245245
- [1262.greatest-sum-divisible-by-three](./problems/1262.greatest-sum-divisible-by-three.md) 🆕
246246
- [1297.maximum-number-of-occurrences-of-a-substring](./problems/1297.maximum-number-of-occurrences-of-a-substring.md) 🆕
247247
- [1310.xor-queries-of-a-subarray](./problems/1310.xor-queries-of-a-subarray.md) 🆕
248+
- [1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance](./problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md) 🆕
248249

249250
#### 困难难度
250251

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# 题目地址(1334. 阈值距离内邻居最少的城市)
2+
3+
https://leetcode-cn.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
4+
5+
## 题目描述
6+
7+
```
8+
有 n 个城市,按从 0 到 n-1 编号。给你一个边数组 edges,其中 edges[i] = [fromi, toi, weighti] 代表 fromi 和 toi 两个城市之间的双向加权边,距离阈值是一个整数 distanceThreshold。
9+
10+
返回能通过某些路径到达其他城市数目最少、且路径距离 最大 为 distanceThreshold 的城市。如果有多个这样的城市,则返回编号最大的城市。
11+
12+
注意,连接城市 i 和 j 的路径的距离等于沿该路径的所有边的权重之和。
13+
14+
 
15+
16+
示例 1:
17+
18+
```
19+
20+
![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbh9v5ygtsj20qo0k0aap.jpg)
21+
22+
```
23+
24+
25+
26+
输入:n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
27+
输出:3
28+
解释:城市分布图如上。
29+
每个城市阈值距离 distanceThreshold = 4 内的邻居城市分别是:
30+
城市 0 -> [城市 1, 城市 2] 
31+
城市 1 -> [城市 0, 城市 2, 城市 3] 
32+
城市 2 -> [城市 0, 城市 1, 城市 3] 
33+
城市 3 -> [城市 1, 城市 2] 
34+
城市 0 和 3 在阈值距离 4 以内都有 2 个邻居城市,但是我们必须返回城市 3,因为它的编号最大。
35+
示例 2:
36+
37+
```
38+
39+
![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbh9vg1w43j20qo0k0js8.jpg)
40+
41+
```
42+
43+
输入:n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
44+
输出:0
45+
解释:城市分布图如上。 
46+
每个城市阈值距离 distanceThreshold = 2 内的邻居城市分别是:
47+
城市 0 -> [城市 1] 
48+
城市 1 -> [城市 0, 城市 4] 
49+
城市 2 -> [城市 3, 城市 4] 
50+
城市 3 -> [城市 2, 城市 4]
51+
城市 4 -> [城市 1, 城市 2, 城市 3] 
52+
城市 0 在阈值距离 4 以内只有 1 个邻居城市。
53+
 
54+
55+
提示:
56+
57+
2 <= n <= 100
58+
1 <= edges.length <= n * (n - 1) / 2
59+
edges[i].length == 3
60+
0 <= fromi < toi < n
61+
1 <= weighti, distanceThreshold <= 10^4
62+
所有 (fromi, toi) 都是不同的。
63+
64+
65+
```
66+
67+
## 思路
68+
69+
这道题的本质就是:
70+
71+
1. 在一个无向图中寻找每两个城镇的最小距离,我们使用 Floyd-Warshall 算法(英语:Floyd-Warshall algorithm),中文亦称弗洛伊德算法,是解决任意两点间的最短路径的一种算法。
72+
2. 筛选最小距离不大于  distanceThreshold 的城镇。
73+
3. 统计每个城镇,其满足条件的城镇有多少个
74+
4. 我们找出最少的即可
75+
76+
Floyd-Warshall 算法的时间复杂度和空间复杂度都是$O(N^3)$, 而空间复杂度可以优化到$O(N^2)$。Floyd-Warshall 的基本思想是对于每两个点之间的最小距离,要么经过中间节点 k,要么不经过,我们取两者的最小值,这是一种动态规划思想,详细的解法可以参考[Floyd-Warshall 算法(wikipedia)](https://zh.wikipedia.org/wiki/Floyd-Warshall%E7%AE%97%E6%B3%95)
77+
78+
## 代码
79+
80+
代码支持:Python3
81+
82+
Python3 Code:
83+
84+
```python
85+
class Solution:
86+
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
87+
# 构建dist矩阵
88+
dist = [[float('inf')] * n for _ in range(n)]
89+
for i, j, w in edges:
90+
dist[i][j] = w
91+
dist[j][i] = w
92+
for i in range(n):
93+
dist[i][i] = 0
94+
for k in range(n):
95+
for i in range(n):
96+
for j in range(n):
97+
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
98+
99+
# 过滤
100+
res = 0
101+
minCnt = float('inf')
102+
for i in range(n):
103+
cnt = 0
104+
for d in dist[i]:
105+
if d <= distanceThreshold:
106+
cnt += 1
107+
if cnt <= minCnt:
108+
minCnt = cnt
109+
res = i
110+
return res
111+
112+
113+
```
114+
115+
## 关键点解析
116+
117+
- Floyd-Warshall 算法
118+
- 你可以将本文给的 Floyd-Warshall 算法当成一种解题模板使用

0 commit comments

Comments
 (0)