Heaters 供暖器

冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。

现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。

所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。

说明:

  1. 给出的房屋和供暖器的数目是非负数且不会超过 25000。
  2. 给出的房屋和供暖器的位置均是非负数且不会超过10^9。
  3. 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
  4. 所有供暖器都遵循你的半径标准,加热的半径也一样。

示例 1:

输入: [1,2,3],[2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。

示例 2:

输入: [1,2,3,4],[1,4]
输出: 1
解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。

思路:

首先对输入的两个数组进行由小到大排序,然后每次找到house[i]对应的heat的包含范围(最近的包含范围,即heat[j]<=house[i]<=heat[j+1]),然后计算house[i]离左近还是右近,取其中最小的距离,这样遍历完一遍house数组后,每个house[i]都对应一组距离,取所有house数组中距离的最大值即使答案。具体思路可以用hashmap<int,hashset>保存,但是这样太麻烦,在参考了discuss部分的代码发现可以不需要O(n)的辅助空间,只需要O(1)即可。每次遍历过程中更新res,并且由于house和heat都已经排序,每次查找不需要从头查找,类似于归并查找中的合并步骤来一次遍历house和heat便可以达到完成。

    int findRadius(vector<int>& houses, vector<int>& heaters) {
	sort(houses.begin(), houses.end());
	sort(heaters.begin(), heaters.end());
	int res=0;
	int j = 0;
	for (int i = 0; i < houses.size(); i++) {
		int tmp = houses[i];
		while (j < (heaters.size() - 1) && abs(heaters[j] - tmp) >= abs(heaters[j + 1] - tmp)) {
			j++;
		}
		res = max(res, abs(heaters[j] - tmp));
	}
	return res;        
    }
其中的
abs(heaters[j] - tmp) >= abs(heaters[j + 1] - tmp)

等于符号必须要有,主要是对应如下的情况:

house:[1,2,2,4,5]
heat: [1,2,2,4,5]

大家自己debug便可以看出如果没有等号会出现错位的情况,导致答案错误。





### LeetCode 475 Heaters Problem Solution and Explanation In this problem, one needs to find the minimum radius of heaters so that all houses can be warmed. Given positions of `houses` and `heaters`, both represented as integer arrays, the task is to determine the smallest maximum distance from any house to its nearest heater[^1]. To solve this issue efficiently: #### Binary Search Approach A binary search on answer approach works well here because increasing the radius monotonically increases the number of covered houses. Start by sorting the list of heaters' locations which allows using binary search for finding closest heater distances quickly. ```python def findRadius(houses, heaters): import bisect houses.sort() heaters.sort() max_distance = 0 for house in houses: pos = bisect.bisect_left(heaters, house) dist_to_right_heater = abs(heaters[pos] - house) if pos < len(heaters) else float('inf') dist_to_left_heater = abs(heaters[pos-1] - house) if pos > 0 else float('inf') min_dist_for_house = min(dist_to_right_heater, dist_to_left_heater) max_distance = max(max_distance, min_dist_for_house) return max_distance ``` This code snippet sorts the lists of houses and heaters first. For each house, it finds the nearest heater either directly or indirectly (to the left side). It calculates the minimal distance between these two options and updates the global maximal value accordingly[^3]. The constraints specify that numbers of houses and heaters do not exceed 25000 while their positions range up to \(10^9\)[^2], making efficient algorithms like binary search necessary due to large input sizes involved.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值