给你一个数组 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)
。如果有多个坐标网络信号一样大,请你返回字典序最小的 非负 坐标。
输入: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 没有别的坐标有更大的信号强度。
class Solution {
public:
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
int xMax = INT_MIN, yMax = INT_MIN;
for (auto &&tower : towers) {
int x = tower[0], y = tower[1];
xMax = max(xMax, x);
yMax = max(yMax, y);
}
int cx = 0, cy = 0;
int maxQuality = 0;
for (int x = 0; x <= xMax; x++) {
for (int y = 0; y <= yMax; y++) {
vector<int> coordinate = {x, y};
int quality = 0;
for (auto &&tower : towers) {
int squaredDistance = getSquaredDistance(coordinate, tower);
if (squaredDistance <= radius * radius) {
double distance = sqrt((double)squaredDistance);
quality += floor((double)tower[2] / (1 + distance));
}
}
if (quality > maxQuality) {
cx = x;
cy = y;
maxQuality = quality;
}
}
}
return {cx, cy};
}
int getSquaredDistance(const vector<int> &coordinate, const vector<int> &tower) {
return (tower[0] - coordinate[0]) * (tower[0] - coordinate[0]) + (tower[1] - coordinate[1]) * (tower[1] - coordinate[1]);
}
};