// 1620. Coordinate With Maximum Network Quality
class Solution {
public int[] bestCoordinate(int[][] towers, int radius) {
int[] ans = new int[2];
int maxNetworkQuality = 0;
int minX = 50;
int maxX = 0;
int minY = 50;
int maxY = 0;
for (int[] tower : towers) {
if (tower[0] > maxX) maxX = tower[0];
if (tower[0] < minX) minX = tower[0];
if (tower[1] > maxY) maxY = tower[1];
if (tower[1] < minY) minY = tower[1];
}
for (int x = minX; x <= maxX; ++x) {
for (int y = minY; y <= maxY; ++y) {
int networkQuality = 0;
for (int[] tower : towers) {
double distance = (Math.sqrt(Math.pow(Math.abs(tower[0] - x), 2) + Math.pow(Math.abs(tower[1] - y), 2)));
if (distance <= radius) {
networkQuality += (int) (tower[2] / (1 + distance));
}
}
if (networkQuality > maxNetworkQuality) {
maxNetworkQuality = networkQuality;
ans[0] = x;
ans[1] = y;
}
}
}
return ans;
}
}
学习笔记:
这是一道枚举算法的题目。
由于题目限制了x和y在50以内,所以只需要遍历这2601个点就可以了。
每一个点计算与所有基站距离,然后将信号强度加总,最后比较。