No need consider boundary condition!! C++


  • 2
    H
    int findRadius(vector<int>& houses, vector<int>& heaters) {
            heaters.push_back(INT_MAX);
            heaters.push_back(INT_MIN);
            sort(heaters.begin(), heaters.end());
            long long res = 0;
            for (p : houses){
                int p1 = lower_bound(heaters.begin(), heaters.end(), p)-heaters.begin();
                int p2 = p1-1;
                long long temp = min( ((long long)heaters[p1]-p), ((long long)p-heaters[p2]));
                res = max(res, temp);
            }
            return res;
    }
    

  • 0
    L

    Brilliant!! Nice using INT_MAX, INT_MIN.


  • 0
    C

    @hualiang2 The code excels the time limit. Any idea?


Log in to reply
 

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