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
| class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { int r = 1e9, l = 0; sort(houses.begin(), houses.end()); sort(heaters.begin(), heaters.end()); while (l < r) { int mid = l + r >> 1; if (check(houses, heaters, mid)) { r = mid; } else { l = mid + 1; } } return l; } bool check(vector<int>& houses, vector<int>& heaters, int x) { int n = houses.size(), m = heaters.size(); for (int i = 0, j = 0; i < n; i++) { while (j < m && houses[i] > heaters[j] + x) j++; if (j < m && heaters[j] - x <= houses[i] && houses[i] <= heaters[j] + x) continue; return false; } return true; } };
|