LeetCode_475. Heaters
475. 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.
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的更多相关文章
- 【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: Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- heaters
https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [Swift]LeetCode475. 供暖器 | 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).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- 475. Heaters
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 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 ...
随机推荐
- Codeforces B. Bad Luck Island(概率dp)
题目描述: Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- js生成随机密码,密码位数自定
话不多说,上代码 function pb(size){ var seed = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N', ...
- stm32中的型号对比——为什么很少用STM32F2,F3?
源自网络 我觉得有三点: 1. F2属于加强版的F1,内核还是cortex M3,只是主频提高到了120MHz(F1是72MHz),但是这点提升没有实质性意义,性能比不上 2. F3是F4的削弱版,一 ...
- iOS系统的两个循环
1.事件循环: 2.显示循环:displaylink:系统定时从gpu缓存获取图片信息显示在屏幕.
- k-mean
import numpy as np from k_initialize_cluster import k_init np.random.seed() class YOLO_Kmeans: def _ ...
- [React] Handle React Suspense Errors with an Error Boundary
Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...
- Ansible自动部署lnmp架构+上线电商
1.首先准备3台机器 ansible机器:192.168.52.34 目标主机:192.168.52.35 目标主机:192.168.52.36 2.关闭防火墙 [root@localhost ~]# ...
- singer tap-minio-csv 使用
使用tap-minio-csv 我们可以将s3 中csv 的文件,通过singer 的target 写到不用的系统中,可以兼容 s3 的存储类型,以下是一个集成minio 的测试,将minio 中的c ...
- SQL进阶-隐式类型转换
一.隐式类型转换 1.隐式类型转换 隐式类型转换: '; 程序可读性差,且依赖数据库的隐式转换规则,如果数据库升级,则程序可能无法正确执行: 有可能会导致索引失效: 有可能会导致意想不到的结果: 显式 ...
- GoCN每日新闻(2019-11-09)
GoCN每日新闻(2019-11-09) 1. Go语言发行10周年庆祝 https://blog.golang.org/10years2. 容器中某Go服务GC停顿经常超过100ms排查 https ...