力扣打卡第十一天 网络信号最好的坐标

网络信号最好的坐标

枚举
首先遍历所有的信号塔,获得所有信号塔的 xxx 坐标和 yyy 坐标的最大值,计算该坐标处接收到的所有网络信号塔的信号强度之和,即该坐标的网络信号。遍历全部坐标之后,即可得到网络信号最好的坐标。
为了确保结果坐标是字典序最小的网络信号最好的坐标,遍历时应分别将 xxx 和 yyy 从小到大遍历,只有当一个坐标的网络信号严格大于最好信号时才更新结果坐标。

class Solution:
    def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
        x_max = max(t[0] for t in towers)
        y_max = max(t[1] for t in towers)
        cx = cy = max_quality = 0
        for x in range(x_max + 1):
            for y in range(y_max + 1):
                quality = 0
                for tx, ty, q in towers:
                    d = (x - tx) ** 2 + (y - ty) ** 2
                    if d <= radius ** 2:
                        quality += int(q / (1 + d ** 0.5))
                if quality > max_quality:
                    cx, cy, max_quality = x, y, quality
        return [cx, cy]

猜你喜欢

转载自blog.csdn.net/qq_46157589/article/details/127643977