class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
int r_ans = 0;
sort(houses.begin(),houses.end());
sort(heaters.begin(),heaters.end());
int i = 0;
for(int house : houses)
{
int t = INT_MAX;
while(i<heaters.size() && house > heaters[i]) i++;
if(i == 0)
{
t = heaters[i] - house;
}
else if(i == heaters.size())
{
t = house - heaters[i-1];
}
else
{
t = min(house - heaters[i-1],heaters[i]-house);
}
cout<<i<<" "<<t<<endl;
r_ans = max(r_ans,t);
}
return r_ans;
}
};
本文介绍了一种使用C++实现的算法,用于计算所有房屋到最近供暖器的最大距离,即供暖半径。该算法首先对房屋位置和供暖器位置进行排序,然后遍历每个房屋,找到离它最近的供暖器,记录并返回最大的距离。
395

被折叠的 条评论
为什么被折叠?



