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. php工厂方法模式(factory method pattern)

    继续练 <?php /* The factory method pattern deals with the problem of creating objects without having ...

  2. 后端将Long类型数据传输到前端出现精度丢失的问题

    当将超过16位的数字传输到前端的时候,就会出现精度丢失的问题,然后我按照网上的几种方法实验的时候,只有一种方法成功了.可能是因为环境等方面的问题. 我这里成功是因为:最后使用的是配置mvc的方式,然后 ...

  3. python应用-n颗骰子的和出现的次数

    from random import randint def roll_dice(n): total=0 for _ in range (n): num=randint(1,6) total+=num ...

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

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

  5. LeetCode 1249. Minimum Remove to Make Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ 题目: Given a string s ...

  6. 搭建java环境时,DOS输入java有反应,javac没反应的解决办法。

    2018-11-12 搭java环境踩了许多坑,之前搭环境时在命令台输入java有反应,javac没反应,后来试了很多方法都一样,然后就把java的所有的环境变量都删了,在控制面板里的卸载程序把所有的 ...

  7. .ckpt文件与.pb文件

    .ckpt文件是旧版本的输出saver.save(sess),相当于现在的.ckpt-data checkpoint文件仅用于告知某些TF函数,这是最新的检查点文件. .ckpt-meta 包含元图, ...

  8. v熬夜是场可怕的自杀

    转载于 https://baike.baidu.com/vbaike/%E7%86%AC%E5%A4%9C%E6%98%AF%E5%9C%BA%E5%8F%AF%E6%80%95%E7%9A%84%E ...

  9. 在WinDbg中显示和搜索std::map内容

    我们希望在WinDbg中自动显示.搜索和过滤std::map对象.std::vectors的脚本相对简单,因为vectors中数据的平面结构:map是更复杂的野兽.具体地说,Visual C++ ST ...

  10. 常见WinDbg问题及解决方案

    当你调试一个程序时,你最不想处理的是调试器不能正常工作.当你试图集中精力跟踪一个bug时,总是会因为次要的问题而被忽略,尤其是当调试器的问题导致你失去一个重新编程或者浪费了大量的时间等待调试器完成它, ...