s4553711
2/22/2018 - 2:54 PM

475.cpp

class Solution {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        if (heaters.size() == 0) return 0;
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        int radius = 0;
        int index = 0;
        for(int i = 0; i < houses.size(); i++) {
            while(index + 1 < heaters.size() && abs(heaters[index+1] -  houses[i]) <= abs(heaters[index] - houses[i])) {
                index++;
            }
            radius = max(radius, abs(heaters[index] - houses[i]));
        } 
        return radius;
    }
};