LeetCode 题解工作台

网络信号最好的坐标

给你一个数组 towers 和一个整数 radius 。 数组 towers 中包含一些网络信号塔,其中 towers[i] = [x i , y i , q i ] 表示第 i 个网络信号塔的坐标是 (x i , y i ) 且信号强度参数为 q i 。所有坐标都是在 X-Y 坐标系内的 整数 坐…

category

2

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·结合·enumeration

bolt

答案摘要

由于坐标点的范围是 $[0,.. 50]$,因此我们可以直接暴力枚举所有的坐标点 $(i, j)$,计算每个坐标点的信号强度,然后找出信号强度最大的坐标点。 时间复杂度 $O(n \times C^2)$,其中 是信号塔的数量,而 是坐标点的范围大小。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·结合·enumeration 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个数组 towers 和一个整数 radius

数组  towers  中包含一些网络信号塔,其中 towers[i] = [xi, yi, qi] 表示第 i 个网络信号塔的坐标是 (xi, yi) 且信号强度参数为 qi 。所有坐标都是在  X-Y 坐标系内的 整数 坐标。两个坐标之间的距离用 欧几里得距离 计算。

整数 radius 表示一个塔 能到达 最远距离 。如果一个坐标跟塔的距离在 radius 以内,那么该塔的信号可以到达该坐标。在这个范围以外信号会很微弱,所以 radius 以外的距离该塔是 不能到达的 。

如果第 i 个塔能到达 (x, y) ,那么该塔在此处的信号为 ⌊qi / (1 + d)⌋ ,其中 d 是塔跟此坐标的距离。一个坐标的 信号强度 是所有 能到达 该坐标的塔的信号强度之和。

请你返回数组 [cx, cy] ,表示 信号强度 最大的 整数 坐标点 (cx, cy) 。如果有多个坐标网络信号一样大,请你返回字典序最小的 非负 坐标。

注意:

  • 坐标 (x1, y1) 字典序比另一个坐标 (x2, y2) 小,需满足以下条件之一:
    • 要么 x1 < x2 ,
    • 要么 x1 == x2 且 y1 < y2 。
  • ⌊val⌋ 表示小于等于 val 的最大整数(向下取整函数)。

 

示例 1:

输入:towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
输出:[2,1]
解释:
坐标 (2, 1) 信号强度之和为 13
- 塔 (2, 1) 强度参数为 7 ,在该点强度为 ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
- 塔 (1, 2) 强度参数为 5 ,在该点强度为 ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
- 塔 (3, 1) 强度参数为 9 ,在该点强度为 ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4
没有别的坐标有更大的信号强度。

示例 2:

输入:towers = [[23,11,21]], radius = 9
输出:[23,11]
解释:由于仅存在一座信号塔,所以塔的位置信号强度最大。

示例 3:

输入:towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 2
输出:[1,2]
解释:坐标 (1, 2) 的信号强度最大。

 

提示:

  • 1 <= towers.length <= 50
  • towers[i].length == 3
  • 0 <= xi, yi, qi <= 50
  • 1 <= radius <= 50
lightbulb

解题思路

方法一:暴力枚举

由于坐标点的范围是 [0,..50][0,.. 50],因此我们可以直接暴力枚举所有的坐标点 (i,j)(i, j),计算每个坐标点的信号强度,然后找出信号强度最大的坐标点。

时间复杂度 O(n×C2)O(n \times C^2),其中 nn 是信号塔的数量,而 CC 是坐标点的范围大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
        mx = 0
        ans = [0, 0]
        for i in range(51):
            for j in range(51):
                t = 0
                for x, y, q in towers:
                    d = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
                    if d <= radius:
                        t += floor(q / (1 + d))
                if t > mx:
                    mx = t
                    ans = [i, j]
        return ans
speed

复杂度分析

指标
时间complexity is O((maxX*maxY)*n) where maxX and maxY are the maximum coordinates and n is the number of towers, since each coordinate may check every tower. Space complexity is O(1) extra beyond input, though temporary storage for distances or maximum tracking is constant.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Watch for missing tie-breaking when multiple coordinates have the same network quality.

  • question_mark

    Confirm that signal calculations use integer division as specified by ⌊qi / (1 + distance)⌋.

  • question_mark

    Expect discussion on whether iterating all coordinates is feasible under small constraints.

warning

常见陷阱

外企场景
  • error

    Forgetting the radius cutoff, leading to including towers beyond reach.

  • error

    Miscomputing Euclidean distance or applying floating-point division incorrectly.

  • error

    Ignoring tie-breaking rules for X and Y when multiple coordinates yield the same quality.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Maximize network quality with non-integral coordinates, requiring more precise distance handling.

  • arrow_right_alt

    Include signal decay factors or weights per tower, adding complexity to the sum computation.

  • arrow_right_alt

    Compute top k coordinates with highest network quality instead of just the maximum.

help

常见问题

外企场景

网络信号最好的坐标题解:数组·结合·enumeration | LeetCode #1620 中等