-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
32 lines (31 loc) · 903 Bytes
/
Solution.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
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int left = 0, right = 1e9;
while (left < right) {
int mid = left + right >> 1;
if (check(houses, heaters, mid))
right = mid;
else
left = mid + 1;
}
return left;
}
bool check(vector<int>& houses, vector<int>& heaters, int r) {
int m = houses.size(), n = heaters.size();
int i = 0, j = 0;
while (i < m) {
if (j >= n) return false;
int mi = heaters[j] - r;
int mx = heaters[j] + r;
if (houses[i] < mi) return false;
if (houses[i] > mx)
++j;
else
++i;
}
return true;
}
};