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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
the straight-forward check whether the next heater is closer than the current. Then I thought I probably don't need abs
if I just use
while heaters[i+1] - x <= x - heaters[i]:更近
That's obviously correct if x
is between the heaters, because then that's the correct distances of x
to the two heaters. Less obviously (but imho not surprisingly) it's also correct if x
isn't between them. Finally, after rewriting it to
while heaters[i] + heaters[i+1] <= 2 * x
[一句话思路]:
头回见 不知道什么才是“覆盖全部”的最小距离:
每个house都取最小距离求并集(最大距离)即可
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 大小顺序不确定的距离 要加绝对值,别忘了
- i 更新为 i +1之后变大了,此时用i。不用原来的i+ 1
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
更新之后用i
for (int house : houses) {
while (i < heaters.length - 1 && heaters[i + 1] + heaters[i] <= 2 * house) {
i++;
}
res = Math.max(res, Math.abs(heaters[i] - house));
}
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int findRadius(int[] houses, int[] heaters) {
//ini: sort
Arrays.sort(houses);
Arrays.sort(heaters);
int res = Integer.MIN_VALUE, i = 0; //update
for (int house : houses) {
while (i < heaters.length - 1 && heaters[i + 1] + heaters[i] <= 2 * house) {
i++;
}
res = Math.max(res, Math.abs(heaters[i] - house));
} return res;
}
}
475. Heaters 加热范围的更多相关文章
- 【leetcode】475. Heaters
problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...
- 【LeetCode】475. Heaters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- 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 ...
- 475 Heaters 加热器
详见:https://leetcode.com/problems/heaters/description/ C++: class Solution { public: int findRadius(v ...
- LeetCode_475. Heaters
475. Heaters Easy Winter is coming! Your first job during the contest is to design a standard heater ...
- Leetcode Tags(8)Binary Search
一.475. Heaters 输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器.如果我们将加热半径设为1,那么所有房屋就都能得到供暖. 输入: [1,2,3,4],[1,4 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- proxifier 注册码 +电脑全局代理设置
L6Z8A-XY2J4-BTZ3P-ZZ7DF-A2Q9C(Portable Edition) 5EZ8G-C3WL5-B56YG-SCXM9-6QZAP(Standard Edition) P4 ...
- MAC OS X常用命令总结2
1. dir:显示某个目录下的子目录与文件. 格式:dir [x:] [Path] [filename][ parameter] 参数解释: /a 显示所有文件夹与文件. /p 分页 ...
- git撤销各种状态下的操作
使用Git时会出现各种各样的问题,下面是几种情况下怎么反悔的操作 一,未加入缓存区,撤销文件修改 git checkout -- file 二,已加入缓存区,撤销文件提交 git reset HEAD ...
- C# 用wps(api v9) 将word转成pdf
我们不产生代码只是代码的搬运工 我们先来看一段跑不起来的代码 ..各种未将对象应用到实例.. using System; using System.Collections.Generic; usin ...
- CENTOS7安装DOCKER步骤以及安装阿里镜像加速后无法正常启动服务的问题2018年1月
本文时间2017年12月7日,比较新,大家可以直接参考.有问题直接评论 我根据菜鸟教程通过yum install docker安装了docker,由于测试发现奇慢无比,所以就安装了阿里云的加速,随后也 ...
- Git&Repo 命令大全 ***
首先理解几个基本概念: origin:默认远程版本库: master:默认开发分支: 查看本地更新状态: git status jiangzhaowei@ubuntu$ git status # On ...
- MySQL 当记录不存在时insert,当记录存在时update(ON DUPLICATE KEY UPDATE, REPLACE语句)
MySQL 当记录不存在时insert,当记录存在时更新 网上基本有三种解决方法. 第一种:示例一:insert多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语 ...
- 杂项-Java:JCP
ylbtech-杂项-Java:JCP JCP(Java Community Process) 是一个开放的国际组织,主要由Java开发者以及被授权者组成,职能是发展和更新. 1. 中文名:jcp 外 ...
- 1118 Birds in Forest
题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...
- PHP5.3.8连接Sql Server SQLSRV30
PHP5.3连接SQL Server就不能用php_mssql.dll了. 网上下载了好多都不行,因为它的版本是5.2的,不能再PHP5.3中使用. 后来听说微软专门为PHP出了自己的dll. 叫做M ...