-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path475.cpp
More file actions
17 lines (17 loc) · 681 Bytes
/
475.cpp
File metadata and controls
17 lines (17 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
int res = 0;
sort(heaters.begin(), heaters.end());
for (auto& house : houses) {
auto it = lower_bound(heaters.begin(), heaters.end(), house); // >=
int index = it - heaters.begin();
if (index < heaters.size() && heaters[index] == house) continue;
int distance = INT_MAX;
if (index != heaters.size()) distance = min(distance, heaters[index] - house);
if (index != 0) distance = min(distance, house - heaters[index - 1]);
res = max(res, distance);
}
return res;
}
};