https://leetcode.com/problems/heaters/description/
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int maxRg = 0, j = 0;
for(int i=0; i<houses.size(); i++) {
int h, minh = abs(heaters[j] - houses[i]);
while(j+1<heaters.size()) {
h = abs(heaters[j+1] - houses[i]);
if(h<=minh) {
minh = min(minh, h);
j++;
}
else {
break;
}
}
maxRg = max(maxRg, minh);
}
return maxRg;
}
No comments:
Post a Comment