【LeetCode】475. Heaters 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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.
题目大意
这道题就是有一个加热器的地址列表,还有一个屋子列表,现在需要让你制定一个加热器的加热半径,使得所有的屋子都能被加热(加热器的位置就是加热器地址列表里的了)。
解题方法
遍历
最朴素的思想。
题目给的不是有序的,一定要先排序,排序了之后,对houses进行遍历,找出大于house的最小的heater,然后求出house据左右的heater的最小距离。然后求出整个的最大距离,即为所求。
这个题的思路是从把heater对house进行覆盖的思路转化成house距离左右heater的最小距离。结果是所有最小距离的最大距离。
class Solution(object):
def findRadius(self, houses, heaters):
"""
:type houses: List[int]
:type heaters: List[int]
:rtype: int
"""
houses.sort()
heaters.sort()
ans = 0
pos = 0
heaters = [float('-inf')] + heaters + [float('inf')]
for house in houses:
while house >= heaters[pos]:
pos += 1
r = min(house - heaters[pos - 1], heaters[pos] - house)
ans = max(ans, r)
return ans
下面这个做法是,如果后面的heater距离当前的房子距离比当前heater更小,那么就一直使用这个最小的距离。C++版本如下:
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
int res = 0;
int pos = 0;
int N = heaters.size();
sort(heaters.begin(), heaters.end());
sort(houses.begin(), houses.end());
for (int i = 0; i < houses.size(); ++i) {
int cur = houses[i];
while (pos < N - 1 && abs(heaters[pos + 1] - cur) <= abs(heaters[pos] - cur)) {
++pos;
}
res = max(res, abs(heaters[pos] - cur));
}
return res;
}
};
参考资料:http://www.cnblogs.com/grandyang/p/6181626.html
日期
2018 年 2 月 4 日
2018 年 11 月 26 日 —— 11月最后一周!
【LeetCode】475. Heaters 解题报告(Python & C++)的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
随机推荐
- cpu的性能测试
#!/bin/bash #user%加上sys%是性能的评判标准 User_sys_a=`sar -u 1 3 |tail -1 |awk '{print $3"+"$5}'|bc ...
- OOM机制
Linux内核根据应用程序的要求分配内存,通常来说应用程序分配了内存但是并没有实际全部使用,为了提高性能,这部分没用的内存可以留作它用,这部分内存是属于每个进程的,内核直接回收利用的话比较麻烦,所以内 ...
- 【Go语言学习笔记】包
包其实是每个大型工程都会使用的模块化工具. 将相关的代码封装成一个包,给其他项目调用,提供不同的功能. GO的设计是将一个文件夹看成一个包,虽然不一定非要用文件夹的名字,但是比较建议. 同一个文件夹下 ...
- c++ cmake及包管理工具conan简单入门
cmake是一个跨平台的c/c++工程管理工具,可以通过cmake轻松管理我们的项目 conan是一个包管理工具,能够自动帮助我们下载及管理依赖,可以配合cmake使用 这是一个入门教程,想深入了解的 ...
- Go语言核心36讲(Go语言实战与应用二十三)--学习笔记
45 | 使用os包中的API (下) 我们在上一篇文章中.从"os.File类型都实现了哪些io包中的接口"这一问题出发,介绍了一系列的相关内容.今天我们继续围绕这一知识点进行扩 ...
- Kafka入门教程(二)
转自:https://blog.csdn.net/yuan_xw/article/details/79188061 Kafka集群环境安装 相关下载 JDK要求1.8版本以上. JDK安装教程:htt ...
- 转 Android Lifecycle、ViewModel和LiveData
转自:https://www.jianshu.com/p/982545e01d0a 1.概述 在I / O '17的时候,其中一个重要的主题是Architecture Components.这是一个官 ...
- Mysql百万级数据索引重新排序
参考https://blog.csdn.net/pengshuai007/article/details/86021689中思路解决自增id重排 方式一 alter table `table_name ...
- Linux基础命令---get获取ftp文件
get 使用lftp登录ftp服务器之后,可以使用get指令从服务器获取文件. 1.语法 get [-E] [-a] [-c] [-O base] rfile [-o lfil ...
- Nodejs源码解析之module
modulejs的导入 Require函数详解 module路径解析 module.js的导入 module.js是由node.js在Nodejs程序启动的时候导入的.module.js中使用的req ...