Skip to content

Commit f7f6b12

Browse files
committed
✨feat: add 1620
1 parent 55f8970 commit f7f6b12

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
| [1608. 特殊数组的特征值](https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/solution/by-ac_oier-z525/) | 简单 | 🤩🤩🤩🤩🤩 |
180180
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
181181
| [1619. 删除某些元素后的数组均值](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/) | [LeetCode 题解链接](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/by-ac_oier-73w7/) | 简单 | 🤩🤩🤩🤩 |
182+
| [1620. 网络信号最好的坐标](https://leetcode.cn/problems/coordinate-with-maximum-network-quality/) | [LeetCode 题解链接](https://leetcode.cn/problems/coordinate-with-maximum-network-quality/solution/by-ac_oier-xtx3/) | 中等 | 🤩🤩🤩🤩 |
182183
| [1624. 两个相同字符之间的最长子字符串](https://leetcode.cn/problems/largest-substring-between-two-equal-characters/) | [LeetCode 题解链接](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/by-ac_oier-73w7/) | 简单 | 🤩🤩🤩🤩 |
183184
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](=https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solution/by-ac_oier-ki3t/) | 简单 | 🤩🤩🤩🤩🤩 |
184185
| [1636. 按照频率将数组升序排序](https://leetcode.cn/problems/sort-array-by-increasing-frequency/) | [LeetCode 题解链接](https://leetcode.cn/problems/sort-array-by-increasing-frequency/solution/by-ac_oier-c3xc/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1620. 网络信号最好的坐标](https://leetcode.cn/problems/coordinate-with-maximum-network-quality/solution/by-ac_oier-xtx3/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「枚举」
6+
7+
8+
9+
给你一个数组 `towers` 和一个整数 `radius`
10+
11+
数组  `towers`  中包含一些网络信号塔,其中 $towers[i] = [x_{i}, y_{i}, q_{i}]$ 表示第 `i` 个网络信号塔的坐标是 $(x_{i}, y_{i})$ 且信号强度参数为 $q_{i}$ 。所有坐标都是在  `X-Y` 坐标系内的 **整数** 坐标。两个坐标之间的距离用 **欧几里得距离** 计算。
12+
13+
整数 `radius` 表示一个塔 能到达 的 最远距离 。如果一个坐标跟塔的距离在 `radius` 以内,那么该塔的信号可以到达该坐标。在这个范围以外信号会很微弱,所以 `radius` 以外的距离该塔是 不能到达的 。
14+
15+
如果第 `i` 个塔能到达 $(x, y)$ ,那么该塔在此处的信号为 `⌊q / (1 + d)⌋` ,其中 `d` 是塔跟此坐标的距离。一个坐标的 信号强度 是所有 能到达 该坐标的塔的信号强度之和。
16+
17+
请你返回数组 $[c_{x}, c_{y}]$ ,表示 信号强度 最大的 整数 坐标点 $(c_{x}, c_{y})$ 。如果有多个坐标网络信号一样大,请你返回字典序最小的 **非负** 坐标。
18+
19+
注意:
20+
21+
* 坐标 `(x1, y1)` 字典序比另一个坐标 `(x2, y2)` 小,需满足以下条件之一:
22+
* 要么 `x1 < x2` ,
23+
* 要么 `x1 == x2` 且 `y1 < y2` 。
24+
* `⌊val⌋` 表示小于等于 `val` 的最大整数(向下取整函数)。
25+
26+
示例 1:
27+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/17/untitled-diagram.png)
28+
```
29+
输入:towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
30+
31+
输出:[2,1]
32+
33+
解释:
34+
坐标 (2, 1) 信号强度之和为 13
35+
- 塔 (2, 1) 强度参数为 7 ,在该点强度为 ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
36+
- 塔 (1, 2) 强度参数为 5 ,在该点强度为 ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
37+
- 塔 (3, 1) 强度参数为 9 ,在该点强度为 ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4
38+
没有别的坐标有更大的信号强度。
39+
```
40+
示例 2:
41+
```
42+
输入:towers = [[23,11,21]], radius = 9
43+
44+
输出:[23,11]
45+
46+
解释:由于仅存在一座信号塔,所以塔的位置信号强度最大。
47+
```
48+
示例 3:
49+
```
50+
输入:towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 2
51+
52+
输出:[1,2]
53+
54+
解释:坐标 (1, 2) 的信号强度最大。
55+
```
56+
57+
提示:
58+
* $1 <= towers.length <= 50$
59+
* $towers[i].length = 3$
60+
* $0 <= x_{i}, y_{i}, q_{i} <= 50$
61+
* $1 <= radius <= 50$
62+
63+
---
64+
65+
### 模拟
66+
67+
观察数据范围:无论是 `towers` 数组大小、坐标 $(x, y)$ 的值域大小,还是最远距离 `k = radius`,取值均不超过 $50$。
68+
69+
因此我们可以直接采用「模拟」的方式进行求解,而不会面临 `TLE``MLE` 的风险。
70+
71+
具体的,我们建立一个大小为 $110 \times 110$ 的棋盘 `g`,用于记录每个坐标点的信号值,即 $g[i][j] = x$ 代表坐标 $(i, j)$ 的信号值为 $x$。
72+
73+
> 其中 $110$ 的大小是利用了「任意坐标 $(x, y)$ 的取值范围不超过 $50$」,同时「最远距离 $k$ 不超过 $50$」并且「最终答案为非负坐标」而定。
74+
75+
随后,我们可以枚举所有 $towers[i] = (a, b, q)$,并检查以该塔为中心点,大小为 $(k + k)^2$ 的矩阵中的所有点(该塔所能贡献信号的所有坐标均落在矩阵中),枚举过程中使用变量 `val` 记录最大信号值,使用 `x``y` 记录答案坐标。
76+
77+
Java 代码:
78+
```Java
79+
class Solution {
80+
public int[] bestCoordinate(int[][] towers, int k) {
81+
int[][] g = new int[110][110];
82+
int x = 0, y = 0, val = 0;
83+
for (int[] t : towers) {
84+
int a = t[0], b = t[1], q = t[2];
85+
for (int i = Math.max(0, a - k); i <= a + k; i++) {
86+
for (int j = Math.max(0, b - k); j <= b + k; j++) {
87+
double d = Math.sqrt((a - i) * (a - i) + (b - j) * (b - j));
88+
if (d > k) continue;
89+
g[i][j] += Math.floor(q / (1 + d));
90+
if (g[i][j] > val) {
91+
x = i; y = j; val = g[i][j];
92+
} else if (g[i][j] == val && (i < x || (i == x && j < y))) {
93+
x = i; y = j;
94+
}
95+
}
96+
}
97+
}
98+
return new int[]{x, y};
99+
}
100+
}
101+
```
102+
TypeScript 代码:
103+
```TypeScript
104+
function bestCoordinate(towers: number[][], k: number): number[] {
105+
const g = new Array<Array<number>>(110)
106+
for (let i = 0; i < 110; i++) g[i] = new Array<number>(110).fill(0)
107+
let x = 0, y = 0, val = 0
108+
for (const t of towers) {
109+
const a = t[0], b = t[1], q = t[2]
110+
for (let i = Math.max(0, a - k); i <= a + k; i++) {
111+
for (let j = Math.max(0, b - k); j <= b + k; j++) {
112+
const d = Math.sqrt((a - i) * (a - i) + (b - j) * (b - j))
113+
if (d > k) continue
114+
g[i][j] += Math.floor(q / (1 + d))
115+
if (g[i][j] > val) {
116+
x = i; y = j; val = g[i][j]
117+
} else if (g[i][j] == val && ((i < x) || (i == x && j < y))) {
118+
x = i; y = j
119+
}
120+
}
121+
}
122+
}
123+
return [x, y]
124+
}
125+
```
126+
Python 代码:
127+
```Python
128+
class Solution:
129+
def bestCoordinate(self, towers: List[List[int]], k: int) -> List[int]:
130+
g = [[0] * 110 for _ in range(110)]
131+
x, y, val = 0, 0, 0
132+
for (a, b, q) in towers:
133+
for i in range(max(0, a - k), a + k + 1):
134+
for j in range(max(0, b - k), b + k + 1):
135+
d = math.sqrt((a - i) * (a - i) + (b - j) * (b - j))
136+
if d > k:
137+
continue
138+
g[i][j] += int(q / (1 + d))
139+
if g[i][j] > val:
140+
val, x, y = g[i][j], i, j
141+
elif g[i][j] == val and ((i < x or (i == x and j < y))):
142+
x, y = i, j
143+
return [x, y]
144+
```
145+
* 时间复杂度:需要 $O(n)$ 的复杂度枚举所有的塔 $towers[i]$;对于每座塔,我们需要枚举以该塔为中心点,大小为 $(k + k)^2$ 的矩阵中的所有坐标。整体复杂度为 $O(n \times k^2)$
146+
* 空间复杂度:$O(M^2)$,其中 $M = 110$ 为棋盘大小
147+
148+
---
149+
150+
### 最后
151+
152+
这是我们「刷穿 LeetCode」系列文章的第 `No.1620` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
153+
154+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
155+
156+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
157+
158+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
159+

0 commit comments

Comments
 (0)