C++_AC


  • 0
    class Solution {
    public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        int res = INT_MIN;
        int i = 0, j = 0;
        while(i < houses.size()){
        //consider two extremely situation: j == n || j == n - 1
            if(j == heaters.size() - 1){
                res = max(res, abs(houses[i] - heaters[j]));
                ++i;
            }else if(j == heaters.size()){
                res = max(res, abs(houses[i] - heaters[j-1]));
                ++i;
            }else if(houses[i] <= heaters[j]){
                res = max(res, heaters[j] - houses[i]);
                ++i;
            }else if(houses[i] > heaters[j+1]){
                ++j;
            }else{
                res = max(res, min(heaters[j+1] - houses[i], houses[i] - heaters[j]));
                ++i;
            }
        }
        return res;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.