LeetCode 1620. 网络信号最好的坐标

148 阅读2分钟

题目描述

给你一个数组 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

思路分析

因为题中固定说 x <= 50 & y <= 50, 所以创建一个 长宽都为51 的棋盘, 然后枚举所有的点位, 再去进行计算

需要注意的是:

  • 信号塔的位置不一定在数组 towers 中
  • 收不到信号塔信号的点位信号强度为 0
  • 最小信号强度为 0
  • 注意返回最小字典序, 字典序的判断别出错

需要注意的点我踩坑了三个, 感兴趣的可以看一下错误分析

代码展示

class Solution {
    public int[] bestCoordinate(int[][] towers, int radius) {
        int x = 0, y = 0, max = 0;
        // 因为 x,y 最大值是50 所以固定棋盘为 51*51
        for (int i = 0; i < 51; i++) {
            for (int j = 0; j < 51; j++) {
                int[] temp = new int[]{i,j};
                int sum = 0;
                for (int k = 0; k < towers.length; k++) {
                    // 计算当前坐标和信号塔的距离是否大于 redius
                    int[] tower = towers[k];
                    double high = Math.sqrt((tower[0] - temp[0]) * (tower[0] - temp[0]) + (tower[1] - temp[1]) * (tower[1] - temp[1]));
                    // 如果大于, 跳出本次循环
                    if (high > radius){
                        continue;
                    }
                    // 如果不大于, 则计算信号强度, 并 ++
                    sum += (int) (tower[2] / (1 + high));
                }
                // 坐标点为 整数坐标点 且 非负
                if (temp[0] % 1 != 0 || temp[1] % 1 != 0){
                    continue;
                }
                
                // 如果信号强度相等, 则判断 字典序最小
                if (max == sum){
                    if (x > temp[0]){
                        x = temp[0];
                        y = temp[1];
                    }else if (x == temp[0] && y > temp[1]){
                        x = temp[0];
                        y = temp[1];
                    }
                }else if (sum > max){   // 信号强度大的更换
                    x = temp[0];
                    y = temp[1];
                    max = sum;
                }
            }
        }
        return new int[]{x,y};
    }
}

错误分析

No.1

信号强度最小应该为0 , 不应该设置为 Integer.MIN_VALUE

No.2

最开始陷入了 坐标点 放置 信号塔 直接从起点就错了, 信号塔不一定是和坐标重合的

image.png

此时的代码:

class Solution {
    public int[] bestCoordinate(int[][] towers, int radius) {
        // 返回值
        int[] result = new int[2];
        // 信号强度之和
        Integer max = 0;
        for (int i = 0; i < towers.length; i++) {
            // 假设 temp 坐标就是信号塔的位置
            int[] temp = towers[i];
            // 当前信号塔距离各点位信号强度之和
            int sum = 0;
            for (int j = 0; j < towers.length; j++) {
                // 计算当前坐标和信号塔的距离是否大于 redius
                int[] tower = towers[j];
                double high = Math.sqrt((tower[0] - temp[0]) * (tower[0] - temp[0]) + (tower[1] - temp[1]) * (tower[1] - temp[1]));
                // 如果大于, 跳出本次循环
                if (high > radius){
                    continue;
                }
                // 如果不大于, 则计算信号强度, 并 ++
                sum += (int) (tower[2] / (1 + high));
            }
            // 坐标点为 整数坐标点 且 非负
            if (temp[0] % 1 != 0 || temp[1] % 1 != 0 || temp[0] < 0 || temp[1] < 0){
                continue;
            }
            // 如果信号强度相等, 则判断 字典序最小
            if (max == sum){
                if (result[0] > temp[0]){
                    result[0] = temp[0];
                    result[1] = temp[1];
                }else if (result[1] > temp[1]){
                    result[0] = temp[0];
                    result[1] = temp[1];
                }
            }else if (sum > max){   // 信号强度大的更换
                result[0] = temp[0];
                result[1] = temp[1];
                max = sum;
            }
        }
        return result;
    }
}

No.3 - 5

一共一个错, 分别是判断 字典序最小 y坐标 的时候忘记 x== 了, 折腾老半天

image.png

提交结果

image.png


本文内容到此结束了

如有收获欢迎点赞👍收藏💖关注✔️,您的鼓励是我最大的动力。

如有错误❌疑问💬欢迎各位大佬指出。

我是 宁轩 , 我们下次再见