Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.
Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.
So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.
Note:
Numbers of houses and heaters you are given are non-negative and will not exceed 25000 .Positions of houses and heaters you are given are non-negative and will not exceed 10^9 .As long as a house is in the heaters’ warm radius range, it can be warmed.All the heaters follow your radius standard and the warm radius will the same.Example 1:
Input: [1,2,3],[2] Output: 1 Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.Example 2:
Input: [1,2,3,4],[1,4] Output: 1 Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.题意:这道题有点复杂,说的是:冬季已经来临。任务是设计一个有固定加热半径的标准供暖器,以便向所有房屋供暖。
现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径,输出供暖器所需要的最小加热半径。
由于房屋和供暖器的位置范围非负,且不超过 10^9 ,在这个范围内进行最多 30 30 30 几次搜索,就可以确定需要的最小供暖器范围:
class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { int n = houses.size(), m = heaters.size(), mn = 0, mx = 1e9; sort(houses.begin(), houses.end()); sort(heaters.begin(), heaters.end()); while (mn < mx) { //0~1e9的范围 int midRadius = mn + (mx - mn) / 2, houseIdx = 0; for (int i = 0; i < m; ++i) { //遍历所有供暖器,判断有多少间房屋在供暖器的范围内 int beginPos = heaters[i] - midRadius, endPos = heaters[i] + midRadius; while (houseIdx < n && (houses[houseIdx] >= beginPos && houses[houseIdx] <= endPos)) ++houseIdx; } if (houseIdx >= n) mx = midRadius; //如果全都在 else mn = midRadius + 1; } return mn; } };上述代码的总时间复杂度为 O ( n log n ) O(n\log n) O(nlogn) ,效率如下:
执行用时:256 ms, 在所有 C++ 提交中击败了5.17% 的用户 内存消耗:25.1 MB, 在所有 C++ 提交中击败了5.13% 的用户为了提高效率,需要缩小搜索范围,做法是对排序后的 heaters.back(), houses[].back() 取其最大位置作为最大范围,但是不能取 heaters.front(), houses.front() 的最小位置,因为最小半径可能为 0 ,比如供暖器和房屋位置相同时。代码如下:
class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { sort(houses.begin(), houses.end()); sort(heaters.begin(), heaters.end()); int n = houses.size(), m = heaters.size(); int mn = 0, mx = max(houses.back(), heaters.back()); while (mn < mx) { int midRadius = mn + (mx - mn) / 2, houseIdx = 0; for (int i = 0; i < m; ++i) { int beginPos = heaters[i] - midRadius, endPos = heaters[i] + midRadius; while (houseIdx < n && (houses[houseIdx] >= beginPos && houses[houseIdx] <= endPos)) ++houseIdx; if (houseIdx >= n) break; } if (houseIdx >= n) mx = midRadius; else mn = midRadius + 1; } return mn; } };效率稍微提高了一些:
执行用时:220 ms, 在所有 C++ 提交中击败了14.37% 的用户 内存消耗:24.9 MB, 在所有 C++ 提交中击败了5.13% 的用户还有办法更高吗?有!我们只需要从小到大排序 heaters[] 数组,然后对 houses[] 中的每间房屋的位置 heaters[i] ,在 heaters[] 数组中二分寻找第一个大于等于 heaters[i] 的供暖器,从而得到房子右边最近的供暖器,如果房子左边也有供暖器,就比较两者到房屋的距离而取较小值(如果只有一边则直接取其与房子的距离),这就是这间房屋所需要的最小供暖器半径。取所有最小供暖器半径的最大值,就是要求的结果。具体代码如下:
class Solution { public: //对于每座房子的前后两个供暖器(只一个供暖器时另外判断),寻找供暖器离房屋的较小距离,并更新当前最大半径 int findRadius(vector<int>& houses, vector<int>& heaters) { sort(heaters.begin(), heaters.end()); int ans = 0; for (int i = 0; i < houses.size(); ++i) { int cur = INT_MAX; //larger就是这座房子右边的供暖器 auto &&larger = lower_bound(heaters.begin(), heaters.end(), houses[i]); if (larger != heaters.end()) //说明房子右边有供暖器 cur = *larger - houses[i]; //计算它们之间的距离 if (larger != heaters.begin()) { //说明房子左边有供暖器 auto smaller = larger - 1; //左边供暖器的位置就是当前larger位置-1 cur = min(cur, houses[i] - *smaller); //更新它们之间的较小值 } ans = max(cur, ans); } return ans; } };效率有所提升:
执行用时:172 ms, 在所有 C++ 提交中击败了55.06% 的用户 内存消耗:24.8 MB, 在所有 C++ 提交中击败了5.13% 的用户