leetcode-475-Heaters
题目描述:
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:
- Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
- Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
- As long as a house is in the heaters' warm radius range, it can be warmed.
- All the heaters follow your radius standard and the warm radius will the same.
Example 1:
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:
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个加热器,然后看这两个加热器哪个距离它更近。
把最小距离存储起来,里面最大的那个数值就是我们要的最小半径。
那么如何找到房子两边的上下两个加热器呢?用二分查找,好像有点慢。
我们可以这样做:
第一次对房子和加热器做循环,从左边开始,找到大于等于房子位置的加热器,加热器位置减去房子位置,得到距离,这是上加热器跟房子的距离。
第二次对房子和加热器做循环,从右边开始,找到小于房子位置的加热器,房子位置减去加热器位置,得到距离,这是下加热器跟房子的距离。
经过两次循环,一次上限减房子位置,一次房子位置减下限,我们就可以直接得到距离。
代码如下:
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=;//max1用在最后,存储最大数值
vector<int> res(s1,INT_MAX);//初始值为INT_MAX
for(int i=,h=;i<s1&&h<s2;)//上限-房子位置
{
if(heaters[h]>=houses[i])
{
res[i]=heaters[h]-houses[i];
i++;
}
else
h++;
}
for(int i=s1-,h=s2-;i>=&&h>=;)//房子位置-下限,跟原本数值比较,存储小的值
{
if(heaters[h]<houses[i])
{
res[i]=min(houses[i]-heaters[h],res[i]);
i--;
}
else
h--;
}
for(int i=;i<s1;i++)
{
if(res[i]>max1)
max1=res[i];
}
return max1;
}
上述代码实测73ms,beats 90.81% of cpp submissions。
leetcode-475-Heaters的更多相关文章
- 【leetcode】475. Heaters
problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- 【LeetCode】475. Heaters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 475 Heaters 加热器
详见:https://leetcode.com/problems/heaters/description/ C++: class Solution { public: int findRadius(v ...
- Java实现 LeetCode 475 供暖器
475. 供暖器 冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房 ...
- 475. Heaters
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 475. Heaters (start binary search, appplication for binary search)
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- 475. Heaters 加热范围
[抄题]: Winter is coming! Your first job during the contest is to design a standard heater with fixed ...
- Leetcode 475.供暖气
供暖气 冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器 ...
- 【leetcode】Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
随机推荐
- 基于Mybatis分页插件PageHelper
基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...
- [C++] Returning values by reference in C++
A C++ program can be made easier to read and maintain by using references rather than pointers. A C+ ...
- python 类变量 在多线程下的共享与释放问题-乾颐堂
最近被多线程给坑了下,没意识到类变量在多线程下是共享的,还有一个就是没意识到 内存释放问题,导致越累越大 1.python 类变量 在多线程情况 下的 是共享的 2.python 类变量 在多线程情况 ...
- razor DisplayNameFor ViewModel为集合时显示列名的问题
@{ViewModel nullModel = null; } @Html.DisplayNameFor(model => nullModel .FullName) https://stacko ...
- SQL序列键
当需要更新表中的数据或像表中插入数据时,在很多情况下需要产生唯一的整数序列键 一:更新列的值为唯一值 原数据如下图: 可以定义一个CTE,返回orerid列的值以及row_number()的计算结果. ...
- 使用word写博客
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- 如何解决Android开发中的【java.lang.unsatisfiedlinkerror findLibrary returned null.】 错误
将脉可寻的功能加入到自己的APP中时,需要在libs文件中添加.so文件和jar包 但是,加入.so文件后,仍然报错 在一番折腾之后,终于解决了,然而解决的方法很奇异- -. 在libs下新建一个ar ...
- recv函数的用法详解
recv函数 int recv( SOCKET s, char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用rec ...
- hibernate:对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决办法
在J2EE框架下开发web网站,这种问题经常遇到,只要我们网上搜一下,就可以看到很多版本的,我整理一下: 第一种可能性解决:看看我的项目:主要 是里面的Structs 1.3 (structs 2) ...
- Linux Guard Service - 前台进程和后台进程切换
把一个正在执行的程序放入后台 [root@localhost 01]# Ctrl+Z 此使程序被移动到后台,但不能继续输出(处于暂停态) [root@localhost 01]# ./test1-1 ...