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 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的更多相关文章
- [LeetCode] Heaters 加热器
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [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算法题-Heaters(Java实现)
这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- 【LeetCode】475. Heaters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 【leetcode】475. Heaters
problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...
- 【leetcode】Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- heaters
https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...
随机推荐
- 《DSP using MATLAB》示例Example5.15
代码: x1 = [1,2,2]; x2 = [1,2,3,4]; y1 = circonvt(x1,x2,5); % N = 5 n1 = 0:1:length(x1)-1; n2 = 0:1:le ...
- 限制Xamarin获取图片的大小
限制Xamarin获取图片的大小在App开发中,经常会使用网络图片.因为这样不仅可以减少App的大小,还可以动态更新图片.但是手机使用网络环境千差万别.当网络环境不是理想的情况下,加载网络图片就是一个 ...
- SOAPUI测试步骤----DataGen TestStep
DataGen TestStep DataGen TestStep可以用来生成数据输入在你 TestCases ,例如数字或日期序列.随机选择等生成的数据作为属性,因此可以转移 和扩张就像任何其他属性 ...
- js闭包Demo
我们先看一个关于Javascript利用循环绑定事件的例子: 例如:一个不确定长度的列表,在鼠标经过某一条的时候改变背景. ﹤!DOCTYPE html PUBLIC "-//W3C// ...
- java分享第十一天(接口测试)
HTTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求( post请求时有一个选项是form-data,或者raw,使用raw可以请求 ...
- [转]关于AS3 Socket和TCP不得不说的三两事
磨刀不误砍柴工,让我们从概念入手,逐步深入. 所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络 ...
- jQuery 中的事件冒泡和阻止默认行为
1.事件冒泡 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...
- windows php 5.5 执行exe 不是有效的win32程序
双击运行php-cgi.exe弹出对话框提示不是有效的win32应用程序.此为版本问题,PHP5.5版本 最低要运行于操作系统版本号最低要6.0 ,而WINDOWS 2003 系统为5.2 因此无法运 ...
- 玩转 H5 下拉上滑动效
按照上面的技术方案实施,具体过程为: 禁用页面顶部下拉事件 ------> 将页面的主体内容用一个DIV容器包含起来,同时复制需要放大处理的内容节点至主体内容之外 ------> 绑 ...
- 合并多个工作薄workbooks到一个工作薄workbook
微软示例教程 微软示例教程 Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim NRo ...