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

posted on 2018-04-21 19:14  lina2014  阅读(118)  评论(0编辑  收藏  举报

导航