Faster C++ without binary search


  • 0
    B
        int findRadius(vector<int>& houses, vector<int>& heaters) {
            sort(heaters.begin(), heaters.end());
            sort(houses.begin(), houses.end());
            int res = 0;
            for(int i=0,j=0;i<houses.size();i++) {
                while(j<heaters.size() && heaters[j]<houses[i]) j++;
                int t;
                if(j==heaters.size()) t=houses[i]-heaters.back();
                else if(j==0) t = heaters[0]-houses[i];
                else t = min(heaters[j]-houses[i], houses[i]-heaters[j-1]);
                res = max(res,t);
            }
            return res;
        }
    

Log in to reply
 

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