//遍历houses数组,对每一个数组的数字,我们在heaters中找能包含这个数字的左右范围,然后看离左右两边谁近取谁的值,如果某个house位置比heaters中最小的数字还小,那么肯定要用最小的heater去cover,反之如果比最大的数字还大,就用最大的数字去cover。对于每个数字算出的半径,我们要取其中最大的值
int findRadius(vector<int>& houses, vector<int>& heaters)
{
int res = 0;
sort(heaters.begin(), heaters.end());
for (int house : houses)
{
auto pos = lower_bound(heaters.begin(), heaters.end(), house);
int dist1 = (pos == heaters.end()) ? INT_MAX : *pos - house;
int dist2 = (pos == heaters.begin()) ? INT_MAX : house - *(--pos);
res = max(res, min(dist1, dist2));
}
return res;
}