475. Heaters

Easy

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.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 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.

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.
package leetcode.easy;

public class Heaters {
public int findRadius(int[] houses, int[] heaters) {
java.util.Arrays.sort(houses);
java.util.Arrays.sort(heaters);
int result = Integer.MIN_VALUE; for (int house : houses) {
int index = java.util.Arrays.binarySearch(heaters, house);
if (index < 0) {
index = -(index + 1);
}
int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;
result = Math.max(result, Math.min(dist1, dist2));
}
return result;
} @org.junit.Test
public void test() {
int[] houses1 = { 1, 2, 3 };
int[] heaters1 = { 2 };
int[] houses2 = { 1, 2, 3, 4 };
int[] heaters2 = { 1, 4 };
System.out.println(findRadius(houses1, heaters1));
System.out.println(findRadius(houses2, heaters2));
}
}

LeetCode_475. Heaters的更多相关文章

  1. 【leetcode】475. Heaters

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

  2. [LeetCode] Heaters 加热器

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

  3. Leetcode: Heaters

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

  4. heaters

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

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

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

  6. [Swift]LeetCode475. 供暖器 | Heaters

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

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

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

  8. 475. Heaters

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 475. Heaters (start binary search, appplication for binary search)

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

随机推荐

  1. python正则表达式(5)--findall、finditer方法

    findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...

  2. CentOS 7.2搭建FastDFS 分布式文件系统,实现高可用集群

    分布式集群搭建结构 双Tracker 2组Group 轮询存储策略 Keepalived+Nginx高可用 Nginx缓存 4个存储节点 一. 集群规划清单 1.安装清单 软件名称 版本 百度云盘存放 ...

  3. 使用 Express Generator快速创建Express应用

    全局安装express-generator npm install express-generator -g 根据需求生成自己需要的模板 生成ejs模板:express demo --view=ejs ...

  4. vector rIterator

    #include<vector> #include<iostream> using namespace std; void main() { vector<int> ...

  5. windows强制删除文件和文件夹

    包括只读类型 Deletes one or more files. DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] namesERASE [/P] [/F] [ ...

  6. [Inside HotSpot] Xcode编译调试OpenJDK12

    编译 下载brew然后安装hg,freetype,ccache $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent. ...

  7. fpm 打包跨平台rpm 包的一个问题

    平时我们开发机器可能使用的是mac 系统,但是部署机器kennel是linux,默认会有一个操作系统的问题, 我们可以在打包的时候指定os版本信息 参考 --rpm-os linux 参考例子 一个构 ...

  8. 第04组alpha冲刺(4/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(4/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文 ...

  9. 《Elasticsearch实战》读书笔记

    遗留问题: 1._source字段和field字段的区别 2.q和search的区别(查询请求中) 3.输入关键字的大小写,参考prefix查询,match_phrase_prefix查询(4.4.2 ...

  10. 对 OAuth2 和非标 DingDing OAuth2 的一次尝试

    印象中工作以来还从来没搞过一次 OAuth2 的接入- -,似乎只停留在愉快的使用阶段.比如现在很多网站都接了 wx 二维码扫码登录或者微博登录.所以一直只是享受着这样的便利,却没有机会自己来搞一搞把 ...