前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-475-Heaters

leetcode-475-Heaters

作者头像
chenjx85
发布2018-05-22 16:27:18
4090
发布2018-05-22 16:27:18
举报

题目描述:

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:

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  3. As long as a house is in the heaters' warm radius range, it can be warmed.
  4. All the heaters follow your radius standard and the warm radius will the same.

Example 1:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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.

要完成的函数:

int findRadius(vector<int>& houses, vector<int>& heaters) 

说明:

1、这道题目给定两个vector,一个存储了房屋的位置,另一个存储了加热器的位置,要求输出加热器的最小半径,使得加热器可以覆盖所有房子。房屋和加热器都是一维水平向量。

2、假设房屋位置和加热器位置如下:(三角形代表房子,圆形代表加热器)

△△△△△△△△△△△△

    ○             ○         ○

那么我们最直接的想法就是找到房子两边的上下两个加热器,比如第3个房子就找第1和第2个加热器,第8个房子就找第2和第3个加热器,然后看这两个加热器哪个距离它更近。

把最小距离存储起来,里面最大的那个数值就是我们要的最小半径。

那么如何找到房子两边的上下两个加热器呢?用二分查找,好像有点慢。

我们可以这样做:

第一次对房子和加热器做循环,从左边开始,找到大于等于房子位置的加热器,加热器位置减去房子位置,得到距离,这是上加热器跟房子的距离。

第二次对房子和加热器做循环,从右边开始,找到小于房子位置的加热器,房子位置减去加热器位置,得到距离,这是下加热器跟房子的距离。

经过两次循环,一次上限减房子位置,一次房子位置减下限,我们就可以直接得到距离。

代码如下:

代码语言:javascript
复制
    int findRadius(vector<int>& houses, vector<int>& heaters) 
    {
        sort(houses.begin(),houses.end());//排序
        sort(heaters.begin(),heaters.end());
        int s1=houses.size(),s2=heaters.size(),max1=0;//max1用在最后,存储最大数值
        vector<int> res(s1,INT_MAX);//初始值为INT_MAX
        for(int i=0,h=0;i<s1&&h<s2;)//上限-房子位置
        {
            if(heaters[h]>=houses[i])
            {
                res[i]=heaters[h]-houses[i];
                i++;
            }  
            else
                h++;
        }
        for(int i=s1-1,h=s2-1;i>=0&&h>=0;)//房子位置-下限,跟原本数值比较,存储小的值
        {
            if(heaters[h]<houses[i])
            {
                res[i]=min(houses[i]-heaters[h],res[i]);
                i--;
            }
            else
                h--;
        }
        for(int i=0;i<s1;i++)
        {
            if(res[i]>max1)
                max1=res[i];
        }
        return max1;
    }

上述代码实测73ms,beats 90.81% of cpp submissions。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档