leetcode 475. 供暖器

138 篇文章 0 订阅

【题目】475. 供暖器

冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。
现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。
所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。

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

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

示例 2:

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

【解题思路1】双指针

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int l_house = houses.length;
        int l_heat = heaters.length;
        //供暖最小编号 > 房屋最大编号
        if (heaters[0] >= houses[l_house - 1]) {
            return heaters[0] - houses[0];
        }
        //供暖最大编号 < 房屋最先编号
        if (heaters[l_heat - 1] <= houses[0]) {
            return houses[l_house - 1] - heaters[l_heat - 1];
        }

        int radius = 0;
        int i=0, j=0;
        //最小房屋 < 最小供暖,初始化radius值
        if (houses[0] < heaters[0]) {
            radius = heaters[0] - houses[0];
        }
        //i后移到第一个供暖,初试化i,保障之后比较时前后都有供暖,比较前后供暖距离
        while (houses[i] <= heaters[j]) {
            i++;
        }
        while (i < l_house && j < l_heat - 1) {
            //如果当前房屋 < 下一个供暖,比较到前后供暖的距离
            if (houses[i] <= heaters[j+1]) {
                radius = Math.max(radius, Math.min(houses[i]-heaters[j], heaters[j+1]-houses[i]));
                i++; //房屋下标后移
            }else{ //否则供暖下标后移
                j++;
            }
        }
        //如果已经到了最后一个供暖,还有房屋未遍历,最小距离就是 最大房屋编号 - 最后大供暖编号
        if (i < l_house) {
            radius = Math.max(radius, houses[l_house-1] - heaters[l_heat-1]);
        }

        return radius;
    }
}

Math.abs(heaters[i] - house) >= Math.abs(heaters[i + 1] - house) 的意思:
houses:1,2,3,4
heaters:1,4
对于 house 1,heater 1 比 heater 4 更接近 house1,所以不将 i 移动到 i + 1
对于 house 2,heater 1 比 heater 4 更接近 house2,所以不将 i 移动到 i + 1
对于 house 3,heater 4 比 heater 1 更接近 house3,所以将 i 移动到 i + 1
对于 house 4,依次类推

//精简写法
class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);

        int res = 0;
        int i = 0;
        for (int house : houses) {
            while (i < heaters.length - 1 &&
                    Math.abs(heaters[i] - house) >= Math.abs(heaters[i + 1] - house)) {
                i++;
            }
            res = Math.max(res, Math.abs(heaters[i] - house));
        }

        return res;
    }
}

【解题思路2】二分查找

class Solution {
    // 二分查找
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(heaters);

        int res = 0;
        
        for (int house : houses) {
            // 二分搜索当前 house 在 heaters 中的位置
            int index = binarySearch(heaters, house);

            if (index < 0) { // 说明没找到,index 等于当前的 house 应该在 heaters 的位置的负数
                // 计算出当前的 house 应该在 heaters 数组中的位置
                index = -(index + 1);
                // 计算当前 house 离左边最近的 heater 的距离
                int leftDist = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
                // 计算当前 house 离右边最近的 heater 的距离
                int rightDist = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;

                // 当前 house 需要的 heater 的半径取 leftDist 和 rightDist 的最小值
                res = Math.max(res, Math.min(leftDist, rightDist)); 
            }
        }

        return res;
    }

    private int binarySearch(int[] heaters, int house) {
        int low = 0;
        int hight = heaters.length - 1;
        while (low <= hight) {
            int mid = (low + hight) >>> 1;

            if (heaters[mid] < house) {
                low = mid + 1;
            } else if (heaters[mid] > house) {
                hight = mid - 1;
            } else {
                return mid;
            }
        }
        return -(low + 1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值