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.

Binary Search My solution:

Be careful in my binary search function, l, r may go out of the range of the array

 public class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int minRange = Integer.MIN_VALUE;
for (int house : houses) {
int range = binarySearch(house, heaters);
minRange = Math.max(minRange, range);
}
return minRange;
} public int binarySearch(int house, int[] heaters) {
int l=0, r=heaters.length-1;
while (l <= r) {
int m = (r-l)/2 + l;
if (heaters[m] == house) return 0;
else if (heaters[m] < house) l = m + 1;
else r = m - 1;
}
if (l >= heaters.length) return Math.abs(heaters[r]-house);
else if (r < 0) return Math.abs(heaters[l]-house);
return Math.abs(heaters[r]-house)<Math.abs(heaters[l]-house)? Math.abs(heaters[r]-house) : Math.abs(heaters[l]-house);
}
}

Solution with the highest vote:

 public class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int result = Integer.MIN_VALUE; for (int house : houses) {
int index = Arrays.binarySearch(heaters, house); // if put each house in heaters array, this is each house's insertion position
if (index < 0) {
index = -(index + 1);
}
int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; //this house's distance with heaters infront of it, maybe none in front
int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; //this house's distance with heaters after it result = Math.max(result, Math.min(dist1, dist2));
} return result;
}
}

Leetcode: Heaters的更多相关文章

  1. [LeetCode] Heaters 加热器

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  2. [Leetcode] Binary search -- 475. Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  3. LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  4. 【LeetCode】475. Heaters 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  5. 【leetcode】475. Heaters

    problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...

  6. 【leetcode】Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. heaters

    https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...

随机推荐

  1. java调用sqlldr导入csv文件数据到临时表

    package cn.com.file;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File; ...

  2. shell example02

    输入值 //相加 add(){ echo "add two agrs..." echo "enter first one: " read arg1 echo & ...

  3. Android的5大组件

    1. Activity组件 Activity组件通常的表现形式是一个单独的界面(screen).每个Activity都是一个单独的类,它扩展实现了Activity基础类.这个类显示为一个由Views组 ...

  4. 阿里云分布式关系数据库DRDS笔记

    1.Join左边的表查询数据越少,性能越好 2.广播表作为Join的驱动表 3.SQL的Limit优化 SELECT * FROM t_order o WHERE o.id IN ( SELECT i ...

  5. java 打印流(PrintStream)

    打印流(PrintStream):打印流可以打印任意类型的数据,而且打印流在打印数据之前会将数据转为字符串在进行打印 PrintStream可以接受文件和其他字节输出流,所以打印流是对普通字节输出流的 ...

  6. 反射-----学习Spring必学的Java基础知识之一

    Java允许通过程序化的方式间接对Class进行操作,Class文件由类装载器装载后,在JVM中将形成一份描述Class结构的元信息对象,通过该元信息对象可以获知Class的结构信息:如构造函数.属性 ...

  7. 【emWin】例程八:绘制位图

    实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1bpeMYpp 密码:wgtp 实验现象:

  8. 关于iis8.5中发布的网站无法连接数据库的解决方案。

    发布的网站在浏览时出现如下提示: “/”应用程序中的服务器错误. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL ...

  9. Android如何做到应用程序图标隐藏,由第三方程序启动

    在你App的AndroidManifest.xml中,将启动页做如下修改 <intent-filter> <action android:name="android.int ...

  10. maxscale

    一.maxscale简介1.MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换.2.官网:https://mariadb ...