forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaters.cpp
More file actions
21 lines (20 loc) · 704 Bytes
/
Heaters.cpp
File metadata and controls
21 lines (20 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end());
int minRadius = 0;
for(int i = 0; i < houses.size(); i++) {
int currRadius = INT_MAX;
auto lbound = lower_bound(heaters.begin(), heaters.end(), houses[i]);
if(lbound != heaters.end()) {
currRadius = *lbound - houses[i];
}
if(lbound != heaters.begin()) {
int prev = *(--lbound);
currRadius = min(currRadius, houses[i] - prev);
}
minRadius = max(minRadius, currRadius);
}
return minRadius;
}
};