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会有重复情况,这时候对于飘红部分就不会往前 ...
随机推荐
- asp.net 数据库面试题(基础)
今天到某公司笔试,数据库考的比较多,但是说老实话,考的也比较基础.现在趁回忆得起来,将数据库知识简单整理如下: 一.建表指令 比如创建一个学生表student,它由学号Sno,姓名Sname,性别Ss ...
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...
- [数据库]redis与redis操作
网上搜了以下redis的入门操作,全TM的关于怎么安装配置和性能特点的. 基本的CRUD(Create, Read, Update, Delete)就谁也没说,简直气疯了. 先记录下自己常用的命令,后 ...
- jQuery对数据和对象的操作
<script type="text/javascript" src="jquery-1.8.2.min.js"></script> & ...
- Unity3d与Android交互
先看下效果 你一定会说,然并卵! 没错,这里只是一个最简单的例子,unity与android activity 互相传参数. 玩过手游的都知道,在你要为你心爱的游戏角色准备花钱买钻石,点击购买的时候, ...
- 李洪强iOS经典面试题140-UI
李洪强iOS经典面试题140-UI UI viewcontroller的一些方法的说明viewDidLoad,viewWillDisappear, viewWillAppear方法的 顺序和作用? ...
- java文件下载,上传,解压方法
1.文件下载(亲测可用) private static final int BUFFER = 2 * 1024;// 缓冲区大小(2k)private boolean isSuccess = true ...
- Android课程---优化ListView列表视图(2)
layout_simple.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- sonarqube插件开发(一) 环境搭建
1. 下载Docker容器 # 最新版本镜像 docker pull sonarqube # 长期支持版镜像 docker pull sonarqube:lts 2. 将已有的插件导出(使用docke ...
- FTP上传文件提示550错误原因分析。
今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...