173.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:

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.您给出的房屋和加热器的数量是非负的,不会超过25000。
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.您给出的房屋和加热器的位置是非负的,不会超过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:

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.唯一的加热器放置在位置2,如果我们使用半径1标准,那么所有的房屋都可以加热。

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.两个加热器放置在位置1和4.我们需要使用半径1标准,然后所有房屋都可以加热。

解答:

方法一:

 1 class Solution {
 2     public int findRadius(int[] houses, int[] heaters) {
 3         Arrays.sort(heaters);
 4         int res=0,n=heaters.length;
 5
 6         for(int house:houses){
 7             int left=0,right=n;
 8             while(left<right){
 9                 int mid=left+(right-left)/2;
10                 if(house>heaters[mid])
11                     left=mid+1;
12                 else
13                     right=mid;
14             }
15
16             int distance1=(right==n) ? Integer.MAX_VALUE:heaters[right]-house;
17             int distance2=(right==0) ? Integer.MAX_VALUE:house-heaters[right-1];
18             res=Math.max(res,Math.min(distance1,distance2));
19         }
20
21         return res;
22     }
23 }

详解:

先对heaters排序,然后用二分搜索法迅速找到heaters中第一个大于等于当前house位置的数,如果这个数存在,可以算出它和house的差值

如房屋house处于位置5,离它最近的heater为3和9,那么5-3=2,9-5=4,radis=2即可满足要求。

原文地址:https://www.cnblogs.com/chanaichao/p/9662869.html

时间: 2024-08-30 10:16:19

173.Heaters的相关文章

Ectra Credit 168,169,170,171,172,173,174

168,169,170讨论了美国政治机制. 171 讲述了游戏业的慈善事业. 172 游戏能带我们到新世界,不过没有设计信息 173 制作不必解释就可以让玩家自动行动的游戏,把手是用来拉的,未来枪械和现在的枪械长得差不多. 174 Fail Faster 有计划比没计划好,之后你才可以修正你的路线. 有点子就说. 在纸上测试游戏机制. 在美术之前就把游戏原型做出来. Ectra Credit 168,169,170,171,172,173,174,布布扣,bubuko.com

475. Heaters

http://www.cnblogs.com/EdwardLiu/p/6197086.html https://leetcode.com/problems/heaters/#/description public int findRadius(int[] houses, int[] heaters) { if (houses == null || houses.length == 0) { return 0; } Arrays.sort(houses); Arrays.sort(heaters)

475.Heaters java

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

Xcode8出现AQDefaultDevice(173):Skipping input stram 0 0 0x0

Xcode 升级至xcode8后 出现的不少的Bug 在Xcode8上一直输出出现AQDefaultDevice(173):Skipping input stram  0 0 0x0 解决方法: 1 选择product-->Scheme-->Edit Scheme 2选择Arguments 3 在Environment Variables 添加一个环境变量 OS_ACTIVITY_MODE 设置为"disable" 就可以啦\(^o^)/~

apt-cyg update --2017-02-17 07:57:24-- http://mirrors.163.com/cygwin//x86_64/setup.bz2 正在解析主机 mirrors.163.com... 123.58.173.185, 123.58.173.186 正在连接 mirrors.163.com|123.58.173.185|:80... 已连接。 已发出 HTT

apt-cyg update --2017-02-17 07:57:24-- http://mirrors.163.com/cygwin//x86_64/setup.bz2 正在解析主机 mirrors.163.com... 123.58.173.185, 123.58.173.186 正在连接 mirrors.163.com|123.58.173.185|:80... 已连接. 已发出 HTTP 请求,正在等待回应... 304 Not Modifi cygwin的mirror后面的 / 去掉

Xcode8出现AQDefaultDevice (173): skipping input stream 0 0 0x0

一直不想升级Xcode,但是没办法项目进度只能升级Xcode8,果然不出所料出现了不少bug, Xcode7运行一直没有问题,但是在Xcode8上一直输出AQDefaultDevice (173): skipping input stream 网上查到解决办法 1.选择 Product -->Scheme-->Edit Scheme 2.选择 Arguments 3.在Environment Variables添加一个环境变量 OS_ACTIVITY_MODE 设置值为"disabl

【推荐】阿海老师几何画板基础视频讲解(第1-73集)

几何画板的教程视频,我比较喜欢 20110323094906334.zip 20110323095954505.zip 20110323101448438.zip 20110323102825991.zip 20110323094916454.zip 20110323100012184.zip 20110323101503418.zip 20110323102855949.zip 20110323094933284.zip 20110323100239611.zip 20110323101516

用1到9这九个数字变成三位数加三位数等于三位数的加法,例如:173+295 =468,一共有多少种情况?

#include "stdafx.h" #include <stdio.h> #include <vector> #include <algorithm> using namespace std; void FindCount(vector<int> &vect,int iPos,int &Count) { if (iPos>8) { int i1 = vect[0] * 100 + vect[1] * 10 + v

Leetcode: 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