-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path475.cpp
executable file
·34 lines (32 loc) · 1.06 KB
/
475.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(houses.begin(),houses.end());
sort(heaters.begin(), heaters.end());
int right = max(houses.back(),heaters.back()) - min(heaters.front(),houses.front());
int left = 0;
while (left < right){
cout << left << right << endl;
int middle = left + (right - left) / 2;
int index = 0;
bool cover = true;
for (auto heater : heaters){
if (index == houses.size()){
break;
}
if (heater - middle > houses[index]){
cover = false;
break;
}
while (index < houses.size() && houses[index] <= heater + middle)
index++;
}
if (cover && index >= houses.size()){
right = middle;
}else{
left = middle + 1;
}
}
return right;
}
};