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);
int res = 0, j = 0;
for (int i = 0; i < houses.length; i++) {
while (j + 1 < heaters.length && Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1])) {
j++;
}
res = Math.max(res, Math.abs(houses[i] - heaters[j]));

}
return res;
}

二分查找法:

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;
}
}

时间: 2024-08-10 19:07:00

475. 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

LeetCode #475 Heaters

Question 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

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

leetcode 475. 供暖器(Heaters)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器的位置.你将输出供暖器的最小加热半径. 说明: 给出的房屋和供暖器的数目是非负数且不会超过 25000. 给出的房屋和供暖器的位置均是非负数且不会超过109. 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖. 所有供暖器都遵循你的半

UVA 475 - Wild Thing(KMP)

UVA 475 - Wild Thing 题目链接 题意:给定一个带通配符的文件名作为格式,后面跟一个文件名,要求输出符合格式的文件名 思路:先把带通配符的文件名根据星号位置进行分解,然后对于每个文件名去判断,判断的方法用KMP,如果格式的每段都能在文件名中不断往后一一匹配上,那么就是可以的,注意考虑开头和结尾没有星号的情况,还有这题就是如果找不到一个合适的,就什么都不输出,包括换行 代码: #include <cstdio> #include <cstring> #include

php 301 重定向 转自http://www.icoa.cn/a/475.html

内容简介 有时候我们的有多个域名指向同一个网站,或者我们更换了网站的网址,那么怎么样将原来网站的流量导入到新网址中呢,那么我们可以用301重定向的方式,而且这种方式是对搜索引擎比较友好的方式.如果首页是PHP做的,可以使用PHP的301重定向的代码…… 相关关键词:PHP   301   跳转   重定向 本文链接:http://www.icoa.cn/a/475.html [复制网址] 以前介绍过301重定向ASP代码,也就是ASP程序可以用代码来实现网址的301跳转,如果我们使用的PHP程序

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

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,