475 Heaters 加热器

详见:https://leetcode.com/problems/heaters/description/

C++:

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

 参考:http://www.cnblogs.com/grandyang/p/6181626.html

原文地址:https://www.cnblogs.com/xidian2014/p/8903107.html