力扣解法汇总475-供暖器

原题链接:力扣


描述:

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

在加热器的加热半径范围内的每个房屋都可以获得供暖。

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

说明:所有供暖器都遵循你的半径标准,加热的半径也一样。

示例 1:

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

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

输入:houses = [1,5], heaters = [2]
输出:3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/heaters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路:

设置三个变量:
* heaterIndex:记录当前对比的heater的位置
* currentRadius:记录当前值的最小半径
* maxRadius:记录所有值的最小半径
* 先对两个数组进行排序,这样两个数组都是递归增加的了。
* 然后遍历houses数组,我们分为三种场景:
* 1.小于heaters[0],那么距离最近的半径只能是house - heaters[0]。
* 2.大于heaters[heaters.length - 1]或者小于heaters[heaters.length - 1]但是heaterIndex值已经读到了最后一个了。
* 3.其余情况。这时候,我们要做一个循环,循环中两次对比,第一次是和headerIndex位置的对比,第二次是和headerIndex+1位置的对比。两者取小的那个。
* 如果headerIndex+1位置的更大,那么就可以跳出循环了,因为继续headerIndex+1的话只会更大。
* 举个例子
* houses = {1,7}
* heaters = {4,5,10}
* houses读到7的时候,第一次循环,先和4比较结果=3,然后和5比较结果=2,则继续循环。直到headerIndex读到最后一个或者headerIndex+1的比较结果比headerIndex位置的更大

代码:

public class Solution475 {

    public int findRadius(int[] houses, int[] heaters) {

        int heaterIndex = 0;
        int currentRadius = 0;
        int maxRadius = 0;

        Arrays.sort(houses);
        Arrays.sort(heaters);

        for (int house : houses) {
            if (house <= heaters[0]) {
                currentRadius = Math.abs(house - heaters[0]);
            } else if (house >= heaters[heaters.length - 1] || heaterIndex == heaters.length - 1) {
                currentRadius = Math.abs(house - heaters[heaters.length - 1]);
            } else {
                int abs = Math.abs(house - heaters[heaterIndex]);
                while (heaterIndex < heaters.length - 1) {
                    int newAbs = Math.abs(house - heaters[heaterIndex + 1]);
                    if (newAbs > abs) {
                        currentRadius = Math.abs(abs);
                        break;
                    } else {
                        abs = newAbs;
                        currentRadius = Math.abs(newAbs);
                        heaterIndex++;
                    }
                }
            }
            maxRadius = Math.max(currentRadius, maxRadius);
        }
        return maxRadius;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失落夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值