No.250 - LeetCode1620. Coordinate With Maximum Network Quality

本文介绍了一种用于确定最佳坐标的算法。该算法通过比较不同塔之间的距离和信号强度来找到信号质量最好的位置。通过对坐标进行排序并计算每个塔对其他塔的影响,最终找到信号最优的坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Solution {
public:
    static bool cmp(vector<int>& a,vector<int>& b){
        if(a[0] == b[0])return a[1] < b[1];
        return a[0] < b[0];
    }

    vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
        int N = towers.size();
        vector<int> ans(N,0);
        sort(towers.begin(),towers.end(),cmp);
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                double r = sqrt((towers[i][0] - towers[j][0])*(towers[i][0] - towers[j][0])+
                        (towers[i][1] - towers[j][1])*(towers[i][1] - towers[j][1]));
                if(r <= double(radius)) ans[i] += int(towers[j][2]/(1+r));
            }
        }
        int mx = -1;
        int loc = 0;
        for(int i=0;i<N;i++){
            if(ans[i] > mx){
                mx = ans[i];
                loc = i;
            }
        }
        vector<int> res(2,0);
        res[0] = towers[loc][0];
        res[1] = towers[loc][1];
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值