1620.网络信号最好的坐标
链接:1620.网络信号最好的坐标
难度:Medium
标签:数组、枚举
简介:请你返回数组 [cx, cy] ,表示 信号强度 最大的 整数 坐标点 (cx, cy) 。如果有多个坐标网络信号一样大,请你返回字典序最小的 非负 坐标。
题解 1 - cpp
- 编辑时间:2022-11-02
- 执行用时:44ms
- 内存消耗:8.5MB
- 编程语言:cpp
- 解法介绍:暴力枚举。
class Solution {
public:
    const int MAX = 51;
    vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
        vector<int> ans(2);
        int maxQ = 0;
        for (int x = 0; x < MAX; x++) {
            for (int y = 0; y < MAX; y++) {
                int q = comp(towers, radius, x, y);
                if (q > maxQ || q == maxQ && (x < ans[0] || ans[0] == x && y < ans[1])) {
                    ans[0] = x;
                    ans[1] = y;
                    maxQ = q;
                }
            }
        }
        return ans;
    }
    int comp(vector<vector<int>>& towers, int radius, int x, int y) {
        int cur = 0;
        for (auto &tower : towers) {
            double d = sqrt(pow(tower[0] - x, 2) + pow(tower[1] - y, 2));
            if (d > radius) continue;
            cur += floor(1.0 * tower[2] / (1 + d));
        }
        return cur;
    }
};