Untitled

 avatar
unknown
plain_text
19 days ago
909 B
1
Indexable
class Solution {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        sort(houses.begin(), houses.end());

        sort(heaters.begin(), heaters.end());

        int lo = 0, hi = 1e9 + 10;

        while(lo < hi) {
            int mid = (lo + hi) / 2;

            if(isCovered(mid, houses, heaters)) {
                hi = mid;
            } else {
                lo = mid;
            }
        }
        return hi;
    }

    bool isCovered(int x, vector<int> &houses, vector<int> &heaters) {
        int indx = 0, ht = 0;

        while(ht < heaters.size() and indx < houses.size()) {
            int loc = houses[indx];
            int left = heaters[ht] - x, right = heaters[ht] + x;
            if(loc >= left and loc <= right) {
                indx += 1;
            } else {
                ht += 1;
            }
        }

        return indx == houses.size();
    }
};
Editor is loading...
Leave a Comment