0475.cpp (636B)
1 class Solution { 2 public: 3 int findRadius(vector<int> &houses, vector<int> &heaters) const { 4 sort(begin(heaters), end(heaters)); 5 sort(begin(houses), end(houses)); 6 7 int res = 0, crnt; 8 const int n = size(houses); 9 const int m = size(heaters); 10 for (int i = 0, j = 0; i < n; i++) { 11 while (j < m && houses[i] >= heaters[j]) 12 j++; 13 14 int crnt = INT_MAX; 15 16 if (j > 0) crnt = houses[i] - heaters[j - 1]; 17 if (j != m) crnt = min(crnt, heaters[j] - houses[i]); 18 19 res = max(res, crnt); 20 } 21 22 return res; 23 } 24 };