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. PAT 乙级 1012.数字分类 C++/Java

    题目来源 给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A​1​​ = 能被 5 整除的数字中所有偶数的和: A​2​​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和, ...

  2. Photon介绍与安装

    Photon光子引擎是一款实时的Socket服务器和开发框架,快速.使用方便.容易扩展. 服务端架构在windows系统平台上,采用C#语言编写. 客户端SDK提供了多种平台的开发API,包括DotN ...

  3. 16、Python面向对象进阶

    一.对象的继承 Python中支持一个类同时继承多个父类 class Parent1: pass class Parent2: pass class Sub1(Parent1, Parent2): p ...

  4. APP——Ruby开源分发平台搭建

    前言 蒲公英,维护后把我上传的包清除了,所以想自己搭建一个用下. 步骤 项目用ruby语言写的,部署起来也很简单支持源码和Docker部署 GitHub | https://github.com/pl ...

  5. xpath用发

    xpath的更多语法: https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-2.0/ms256039(v=vs ...

  6. 数据分析 - pandas

    简介 pandas是一个强大的Python数据分析的工具包,它是基于Numpy构建的,正因pandas的出现,让Python语言也成为使用最广泛而且强大的数据分析环境之一. Pandas的主要功能: ...

  7. 2.Vue.js 是什么

    Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架. 与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用. Vue 的核心库只关注视图层,不仅易于上手,还便 ...

  8. LeetCode 1081. Smallest Subsequence of Distinct Characters

    原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...

  9. Python I/O编程 --读写文件、StringIO/ BytesIO

    I/O编程 Input/Output  输入/输出 Stream(流)是一个很重要的概念,可以把流想象成一个水管,数据就是水管里的水 Input Stream就是数据从外面(磁盘.网络)流进内存,Ou ...

  10. luogu 2742 二维凸包

    链接 luogu 模板一 上下利用斜率求凸包然后合并. #include <bits/stdc++.h> using namespace std; const int N=10005; c ...