​LeetCode刷题实战475:供暖器

简介: 算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 供暖器,我们先来看题面:https://leetcode-cn.com/problems/heaters/

Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.


Every house can be warmed, as long as the house is within the heater's warm radius range.


Given the positions of houses and heaters on a horizontal line, return the minimum radius standard of heaters so that those heaters could cover all houses.


Notice that all the heaters follow your radius standard, and the warm radius will the same.

冬季已经来临。你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。在加热器的加热半径范围内的每个房屋都可以获得供暖。现在,给出位于一条水平线上的房屋 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

解题

用二分查找法:前提是有序,所以我们先排序。

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(heaters);
        int ans=-1;
        for(int house:houses){
            ans=Math.max(ans,findMinDest(house,heaters));
        }
        return ans;
    }
    private int findMinDest(int house,int[] heaters){
        int len=heaters.length-1;
        if(house>=heaters[len]) return (house-heaters[len]);
        if(house<=heaters[0]) return (heaters[0]-house);
        int des=Integer.MAX_VALUE;
        int left=0;
        int right=heaters.length-1;
        while(left<=right){
            int mid=left+((right-left)>>1);//这个地方当时括号错了,调了半天。
            if(heaters[mid]==house) return 0;
            if(heaters[mid]<house){
              //如果当前house>heater[mid],那我们向右缩小范围
                des=Math.min(des,Math.abs(house-heaters[mid]));
                left=mid+1;
            }else{
            //当前house<heater[mid],那我们向左缩小范围
                des=Math.min(des,Math.abs(house-heaters[mid]));
                right=mid-1;
            }
        }
        return des;
    }
}

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。


相关文章
|
4天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
8 0
|
4天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
9 0
|
5天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
21 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
5天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
25 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
5天前
|
存储 算法 测试技术
|
5天前
|
算法 C语言 C++
|
存储 算法 C语言
C语言刷题~Leetcode与牛客网简单题
C语言刷题~Leetcode与牛客网简单题
|
21天前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
13 0
|
21天前
|
Java
刷题之Leetcode19题(超级详细)
刷题之Leetcode19题(超级详细)
14 0