提示:所有塔都是正坐标,那么信号最好的点一定是正坐标,不用考虑负坐标。
遍历 { x ( 0 , 50 ) , y ( 0 , 50 ) } \{x(0,50),y(0,50)\} {x(0,50),y(0,50)}的点, 2601 2601 2601个点。和 a u t o p : t o w e r s auto\ p:towers auto p:towers比较。 2601 × 50 = 130050 2601\times50 = 130050 2601×50=130050。问题规模合适。
暴力循环天然按照字典序遍历,所以只要保存第一次出现的 m a x max max的坐标,即为所求。
class Solution {
public:
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
int max = 0;//最大信号
vector<int> ans={0,0};
for(int x = 0;x<=50;x++)
for(int y = 0;y<=50;y++){
int q=0;//q当前信号
for(auto p:towers){//遍历每个towers
double d = sqrt((p[0]-x)*(p[0]-x)+(p[1]-y)*(p[1]-y));
if(d<=radius) q += p[2] / (1 + d);
}
if(max<q){
max = q;
ans = {x,y};
}
}
return ans;
}
};
理解思路很重要!
欢迎读者在评论区留言,作为日更博主,看到就会回复的。
